All Data Structures Namespaces Files Functions Variables
Router Class Reference

## Public Member Functions

| __construct ($server, $context)
| init ($server)
| serveUrl ()
| routeAdd ($route, $controller)
| overrideUrl ($type, $value)
| __get ($key)

## Private Member Functions

| createController ($className, $params)
| routeMatch ($request, $route)
| prepareUrl ()
| doRouteAdd ($route, $controller)

## Private Attributes

| $rootPath
| $rootUrl
| $requestUrl
| $routes
| $context
| $routeParams
| $overridden

## Detailed Description

Will manager new connections to the server and try to match the requests and the controllers

Definition at line 9 of file Router.php.

## Constructor & Destructor Documentation

__construct | ( |  | $server,
|  |  | $context
| ) |  |

Create the router, initialize url and path

Definition at line 82 of file Router.php.

{
$pos = strrpos($server["SCRIPT_NAME"], "/");
$relativePath = (($pos === FALSE) ? "" : substr($server["SCRIPT_NAME"], 0, $pos));
$this->rootPath = $server["DOCUMENT_ROOT"] . $relativePath . "/";
$this->rootUrl = $server["REQUEST_SCHEME"] . "://" . $server["HTTP_HOST"] . $relativePath ."/";
$this->requestUrl = substr($server["REQUEST_URI"], count($this->rootUrl) -1);
$this->context = $context;
$this->routes = array();
$this->overridden = null;
}
Tools\Router\$context
$context
Definition: Router.php:41

## Member Function Documentation

__get | ( |  | $key | ) |

Getter

Definition at line 246 of file Router.php.

{
if (isset($this->overridden) && in_array($key, array("modulesPath")))
return $this->overridden[$key];
switch ($key)
{
case "rootPath": return $this->rootPath; break;
case "rootUrl": return $this->rootUrl; break;
case "modulesPath": return $this->rootPath."content/modules/"; break;
case "modulesUrl": return $this->rootUrl."content/modules/"; break;
case "themesPath": return $this->rootPath."content/theme/"; break;
case "themesUrl": return $this->rootUrl."content/theme/"; break;
}
throw new \Exception("Cannot access attribute {$key}");
}
Tools\Router\$rootPath
$rootPath
Definition: Router.php:16
Tools\Router\$rootUrl
$rootUrl
Definition: Router.php:23

createController | ( |  | $className,
|  |  | $params
| ) |  |

private

Create and return Controller for the given route

Parameters

  | $className |
  array | $routeParameters |

Returns
  on succes, false otherwise

Definition at line 142 of file Router.php.

{
if (!class_exists($className))
return false;
$this->routeParams = $params;
$result = null;
try
{
$result = new $className($this->context, $params);
if (!($result instanceof \Tools\AController))
return false;
}
catch (\Exception\Error404 $e)
{
return false;
}
return $result;
}
Exception
Definition: Error404.php:3
Tools
Definition: AController.php:3

doRouteAdd | ( |  | $route,
|  |  | $controller
| ) |  |

private

Add a route to the internal route list Internal procedure

Definition at line 205 of file Router.php.

{
$this->routes[] = array($route, $controller);
}

init | ( |  | $server | ) |

Called after database initialization Check the site url and redirect user if the HOST does not match If the site url is not defined in database, do not redirect

Definition at line 99 of file Router.php.

{
$siteUrl = \Entity\Config::getConfig(null, "siteUrl");
// @codeCoverageIgnoreStart
// This code is tested under another process
if ($siteUrl != $server["HTTP_HOST"] && $siteUrl !== null)
{
header("location: http://{$siteUrl}{$server['REQUEST_URI']}");
die;
}
// @codeCoverageIgnoreEnd
}
Entity\Config\getConfig
static getConfig($lang=null, $key=null, $defaultValue=null)
Definition: Config.php:43

overrideUrl | ( |  | $type,
|  |  | $value
| ) |  |

Override url This SHOULD not be called for security purpose

Parameters

  string | $type | the url type to override
  string | $value | the new value Will fail if called from a controller

Returns
  boolean false on failure

Definition at line 235 of file Router.php.

{
if (!$this->context->isTestingEnvironment())
return false;
$this->overridden[$type] = $value;
return true;
}

prepareUrl | ( |  | ) |

private

Append local routes to router Will load CMS pages, categories page, products page, cart pages, etc.

Definition at line 193 of file Router.php.

{
$fetcher = new \Entity\Cms();
$pages = $fetcher->selects(null, array("order"));
foreach ($pages as $i)
$this->doRouteAdd($i->shurl, $i->controller);
}
Tools\Router\doRouteAdd
doRouteAdd($route, $controller)
Definition: Router.php:205

routeAdd | ( |  | $route,
|  |  | $controller
| ) |  |

Parameters

  string | $route | Uri to match the controller Uri can be formatted as '/:param/static'. expl. '/product/:id/'
  string | $controller | Controller class name. new $controller() MUST return a instance

Add a route and a Controller to the list Can only be called from `routerSetup' hook

Definition at line 220 of file Router.php.

{
if (!$this->context->hookManager->isInHook("routerSetup"))
throw new \Exception("You can only add routes from `routerSetup' hook");
$this->doRouteAdd($route, $controller);
}
Tools\Router\doRouteAdd
doRouteAdd($route, $controller)
Definition: Router.php:205
Exception
Definition: Error404.php:3

routeMatch | ( |  | $request,
|  |  | $route
| ) |  |

private

Check if the request match route

Parameters

  array | $request | User request
  array | $route | Route to check

Returns
  array on success, false on failure

Definition at line 167 of file Router.php.

{
$i = count($request);
$params = array();
if ($i != count($route))
return false;
while ($i)
{
$i--;
if ($route[$i] == '' && $request[$i] == '')
continue;
if ($route[$i] == '' || $request[$i] == '')
return false;
if ($route[$i][0] != ':' && ($route[$i] != $request[$i]))
return false;
if ($route[$i][0] == ':')
$params[$route[$i]] = $request[$i];
$params[$i -1] = $request[$i];
}
return array_reverse($params);
}

serveUrl | ( |  | ) |

Returns
  controller /core/controller/AController.php Match request to a controller return FALSE on failure (eg. 404)

Definition at line 118 of file Router.php.

{
//TODO trigger hook GET, POST
$this->prepareUrl();
$requestParams = explode("/", $this->requestUrl);
foreach ($this->routes as $i)
{
$routeParams = explode("/", $i[0]);
$p = $this->routeMatch($requestParams, $routeParams);
if ($p === false)
continue;
$controller = $this->createController($i[1], $p);
if ($controller)
return $controller;
}
return false;
}
Tools\Router\prepareUrl
prepareUrl()
Definition: Router.php:193
Tools\Router\$routeParams
$routeParams
Definition: Router.php:51
Tools\Router\createController
createController($className, $params)
Definition: Router.php:142
Tools\Router\routeMatch
routeMatch($request, $route)
Definition: Router.php:167

## Field Documentation

Tools Context $context

private

/core/tools/Context.php Contains website's informations

Definition at line 41 of file Router.php.

array $overridden

private

contains url and paths to rewrite

Definition at line 57 of file Router.php.

string $requestUrl

private

Contains request

Definition at line 29 of file Router.php.

string $rootPath

private

Contains the application's root path (ex: http://myshop.com/) with trailing slash Can be accessed read-only via $instance->rootPath

Contains the application's root path (ex: /srv/http/myshop/) with trailing slash Can be accessed read-only via $instance->rootUrl

Definition at line 16 of file Router.php.

$rootUrl

private

Definition at line 23 of file Router.php.

$routeParams

private

Definition at line 51 of file Router.php.

$routes

private

Definition at line 34 of file Router.php.

The documentation for this class was generated from the following file:

- core/tools/Router.php