ecom
E-commerce cms
 All Data Structures Namespaces Files Functions Variables Pages
Public Member Functions | Private Member Functions | Private Attributes
Router Class Reference

Public Member Functions

 __construct ($context)
 
 init ()
 
 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 (   $context)

Create the router, initialize url and path

Definition at line 76 of file Router.php.

77  {
78  $pos = strrpos($_SERVER["SCRIPT_NAME"], "/");
79  $relativePath = (($pos === FALSE) ? "" : substr($_SERVER["SCRIPT_NAME"], 0, $pos));
80  $this->rootPath = $_SERVER["DOCUMENT_ROOT"] . $relativePath . "/";
81  $this->rootUrl = $_SERVER["REQUEST_SCHEME"] . "://" . $_SERVER["HTTP_HOST"] . $relativePath ."/";
82  $this->requestUrl = substr($_SERVER["REQUEST_URI"], count($this->rootUrl) -1);
83  $this->context = $context;
84  $this->routes = array();
85  }

Member Function Documentation

__get (   $key)

Getter

Definition at line 219 of file Router.php.

220  {
221  switch ($key)
222  {
223  case "rootPath": return $this->rootPath; break;
224  case "rootUrl": return $this->rootUrl; break;
225  case "modulesPath": return $this->rootPath."content/modules/"; break;
226  case "modulesUrl": return $this->rootUrl."content/modules/"; break;
227  case "themesPath": return $this->rootPath."content/theme/"; break;
228  case "themesUrl": return $this->rootUrl."content/theme/"; break;
229  }
230  throw new \Exception("Cannot access attribute {$key}");
231  }
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.

133  {
134  if (!class_exists($className))
135  return false;
136  try
137  {
138  $this->routeParams = $params;
139  $result = new $className($this->context, $params);
140  }
141  catch (\Exception\Error404 $e)
142  {
143  return false;
144  }
145  if (!($result instanceof \Tools\AController))
146  return false;
147  return $result;
148  }
doRouteAdd (   $route,
  $controller 
)
private

Add a route to the internal route list Internal procedure

Definition at line 194 of file Router.php.

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

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.

93  {
94  $siteUrl = \Entity\Config::getConfig(null, "siteUrl");
95  if ($siteUrl != $_SERVER["HTTP_HOST"] && $siteUrl !== null)
96  {
97  header("location: http://{$siteUrl}{$_SERVER['REQUEST_URI']}");
98  die;
99  }
100  }
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.

183  {
184  $fetcher = new \Entity\Cms();
185  $pages = $fetcher->selects(null, array("order"));
186  foreach ($pages as $i)
187  $this->doRouteAdd($i->shurl, $i->controller);
188  }
doRouteAdd($route, $controller)
Definition: Router.php:194
routeAdd (   $route,
  $controller 
)
Parameters
string$routeUri to match the controller Uri can be formatted as '/:param/static'. expl. '/product/:id/'
string$controllerController 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.

210  {
211  if (!$this->context->hookManager->isInHook("routerSetup"))
212  throw new \Exception("You can only add routes from `routerSetup' hook");
213  $this->doRouteAdd($route, $controller);
214  }
doRouteAdd($route, $controller)
Definition: Router.php:194
routeMatch (   $request,
  $route 
)
private

Check if the request match route

Parameters
array$requestUser request
array$routeRoute to check
Returns
array on success, false on failure

Definition at line 156 of file Router.php.

157  {
158  $i = count($request);
159  $params = array();
160  if ($i != count($route))
161  return false;
162  while ($i)
163  {
164  $i--;
165  if ($route[$i] == '' && $request[$i] == '')
166  continue;
167  if ($route[$i] == '' || $request[$i] == '')
168  return false;
169  if ($route[$i][0] != ':' && ($route[$i] != $request[$i]))
170  return false;
171  if ($route[$i][0] == ':')
172  $params[$route[$i]] = $request[$i];
173  $params[$i -1] = $request[$i];
174  }
175  return array_reverse($params);
176  }
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.

109  {
110  //TODO trigger hook GET, POST
111  $this->prepareUrl();
112  $requestParams = explode("/", $this->requestUrl);
113  foreach ($this->routes as $i)
114  {
115  $routeParams = explode("/", $i[0]);
116  $p = $this->routeMatch($requestParams, $routeParams);
117  if (!$p)
118  continue;
119  $controller = $this->createController($i[1], $p);
120  if ($controller)
121  return $controller;
122  }
123  return false;
124  }
createController($className, $params)
Definition: Router.php:132
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: