# ecom: Router Class Reference

All Data Structures Namespaces Files Functions Variables
Router Class Reference

## Public Member Functions

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

## Private Member Functions

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

## Private Attributes

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

## 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 76 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();
}
Tools\Router\$context
$context
Definition: Router.php:41

## Member Function Documentation

__get | ( |  | $key | ) |

Getter

Definition at line 219 of file Router.php.

{
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 132 of file Router.php.

{
if (!class_exists($className))
return false;
try
{
$this->routeParams = $params;
$result = new $className($this->context, $params);
}
catch (\Exception\Error404 $e)
{
return false;
}
if (!($result instanceof \Tools\AController))
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 194 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 92 of file Router.php.

{
$siteUrl = \Entity\Config::getConfig(null, "siteUrl");
if ($siteUrl != $server["HTTP_HOST"] && $siteUrl !== null)
{
header("location: http://{$siteUrl}{$server['REQUEST_URI']}");
die;
}
}
Entity\Config\getConfig
static getConfig($lang=null, $key=null, $defaultValue=null)
Definition: Config.php:23

prepareUrl | ( |  | ) |

private

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

Definition at line 182 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:194

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 209 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:194
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 156 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 108 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)
continue;
$controller = $this->createController($i[1], $p);
if ($controller)
return $controller;
}
return false;
}
Tools\Router\prepareUrl
prepareUrl()
Definition: Router.php:182
Tools\Router\$routeParams
$routeParams
Definition: Router.php:51
Tools\Router\createController
createController($className, $params)
Definition: Router.php:132
Tools\Router\routeMatch
routeMatch($request, $route)
Definition: Router.php:156

## Field Documentation

Tools Context $context

private

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

Definition at line 41 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