class Router (View source)

\Yaf\Router is the standard framework router. Routing is the process of taking a URI endpoint (that part of the URI which comes after the base URI: see \Yaf\Request_Abstract::setBaseUri()) and decomposing it into parameters to determine which module, controller, and action of that controller should receive the request. This values of the module, controller, action and other parameters are packaged into a \Yaf\Request_Abstract object which is then processed by \Yaf\Dispatcher. Routing occurs only once: when the request is initially received and before the first controller is dispatched. \Yaf\Router is designed to allow for mod_rewrite-like functionality using pure PHP structures. It is very loosely based on Ruby on Rails routing and does not require any prior knowledge of webserver URL rewriting


Default Route

\Yaf\Router comes pre-configured with a default route \Yaf\Route_Static, which will match URIs in the shape of controller/action. Additionally, a module name may be specified as the first path element, allowing URIs of the form module/controller/action. Finally, it will also match any additional parameters appended to the URI by default - controller/action/var1/value1/var2/value2.


Note:

Module name must be defined in config, considering application.module="Index,Foo,Bar", in this case, only index, foo and bar can be considered as a module name. if doesn't config, there is only one module named "Index".


** See examples by opening the external documentation

Properties

protected Route_Interface[] $_routes
protected string $_current

Methods

__construct()

No description

Router|false
addRoute(string $name, Route_Interface $route)

by default, \Yaf\Router using a \Yaf\Route_Static as its default route. you can add new routes into router's route stack by calling this method.


the newer route will be called before the older(route stack), and if the newer router return TRUE, the router process will be end. otherwise, the older one will be called.

Router|false
addConfig(Config_Abstract $config)

Add routes defined by configs into \Yaf\Router's route stack

Router|false
route(Request_Abstract $request)

No description

getRoute(string $name)

Retrieve a route by name, see also \Yaf\Router::getCurrentRoute()

getRoutes()

No description

string
getCurrentRoute()

Get the name of the route which is effective in the route process.


Note:

You should call this method after the route process finished, since before that, this method will always return NULL.

Details

__construct()

No description

Router|false addRoute(string $name, Route_Interface $route)

by default, \Yaf\Router using a \Yaf\Route_Static as its default route. you can add new routes into router's route stack by calling this method.


the newer route will be called before the older(route stack), and if the newer router return TRUE, the router process will be end. otherwise, the older one will be called.

Parameters

string $name
Route_Interface $route

Return Value

Router|false

return FALSE on failure

Router|false addConfig(Config_Abstract $config)

Add routes defined by configs into \Yaf\Router's route stack

Parameters

Config_Abstract $config

Return Value

Router|false

return FALSE on failure

Router|false route(Request_Abstract $request)

No description

Parameters

Request_Abstract $request

Return Value

Router|false

return FALSE on failure

Route_Interface getRoute(string $name)

Retrieve a route by name, see also \Yaf\Router::getCurrentRoute()

Parameters

string $name

Return Value

Route_Interface

Route_Interface[] getRoutes()

No description

Return Value

Route_Interface[]

string getCurrentRoute()

Get the name of the route which is effective in the route process.


Note:

You should call this method after the route process finished, since before that, this method will always return NULL.

Return Value

string

the name of the effective route.