Router Class

The Router class determines which controller needs to be loaded, based on the request passed to it, and the routes defined.

process(\Request $request, $route = true)

The process method takes a request object, and will try to find a route match for the URI contained in the request. If no route can be matched, the router will create a new route based on the information in the URI, and following the controller/method or module/controller/method URI scheme. It will not check if the determined route is valid!

Static Yes
Parameters
Param Default Description
$request required A valid Request object.
$route true If 'true', the loaded routes will be used to find a match. If 'false', the loaded routes will be ignored and a new route will be constructed. This is used for HMVC calls to controllers that are not routeable from the URL.
Returns Route object, or null if no valid route object could be constructed

Under normal circumstances you won't be needing this method, it is used internally in the framework to load controllers.

add($path, $options = null, $prepend = false, $case_sensitive = null)

The add method allows you to manually add new routes at runtime. You might need this if you want to load controllers that don't follow the normal URI scheme, or if you want to load routes from other sources then the routes.php configuration files.

Static Yes
Parameters
Param Default Description
$path required The URI path to be matched. This is the left side of your routes.php route definition.
$options string|array|Route Either the translation for $path, an array for verb routing (like the right side of your routes.php rout definition) or an instance of Route.
$prepend false If true, the new route will be prepended to the already loaded routes.
$case_sensitive null If false, the new route will be matched against the URI without regards to case. If true, the match will be case sensitive. If not given, the configured default will be used.
Returns void
Example
// load a new route for 'this/that' => 'something/else'
Router::add('this/that', 'something/else');

// force it to be case sensitive
Router::add('this/that', 'something/else', true);

// manually create a Route instance which is treated as not case sensitive
$path = 'this/that';
Router::add($path, new Route($path, 'something/else', false));

get($name, $named_params = array())

The get method allows you to do reverse routing for a named route. Based on the name passed, it will retrieve the defined path for it, and return a URI that you can use in your views. This means that if you change your routes, the links in your views will be updated accordingly.

Static Yes
Parameters
Param Default Description
$name null Name of the route to load.
$named_params array() Array of parameters to be added to the URI created.
Returns bool|array
Example
// if your routes are defined like this:
return array(
	'_root_'  => 'welcome/index',  // The default route
	'_404_'   => 'welcome/404',    // The main 404 route

	'hello(/:name)?' => array('welcome/hello', 'name' => 'hello'),
);

// this call will return 'http://your_base_url/welcome/hello'
echo Router::get('hello');

You can also do this if your route contains a named parameters, a regex, or a combination of both.

// if your route is defined like this:
return array(
    'thread/(?P<thread_id>\d+?)/post' => array('post', 'name' => 'post'),
);

// these will return 'thread/1/post':
echo Router::get('post', array('thread_id' => 1));
echo Router::get('post', array('$1' => 1));
echo Router::get('post', array(1));

// if your route is defined like this:
return array(
    'country/(?P<country>\d+?)/state/(?P<state>\d+?)/location' => array('location', 'name' => 'location'),
);

// these will return 'country/japan/state/tokyo/location':
echo Router::get('location', array('country' => 'japan', 'state' => 'tokyo'));
echo Router::get('location', array('$1' => 'japan', '$2' => 'tokyo'));
echo Router::get('location', array('japan', 'tokyo'));
echo Router::get('location', array('country' => 'japan', 'tokyo'));
echo Router::get('location', array('$1' => 'japan', 'tokyo'));

Note that if your route contains a mix of a traditional regex and a named parameter or a regex shortcut, they will be replaced together, which might lead to unexpected results.

// if your route is defined like this:
return array(
	'hello/(:name)(/:segment)' => array('welcome/user', 'name' => 'user'),
);

// note that "(/:segment)" will be replaced entirely by "article", so
// this call will return 'http://your_base_url/welcome/user/johnarticle' !
echo Router::get('user', array('name' => 'john', 'article'));

delete($path, $case_sensitive = null)

The delete method allows you to manually delete existing routes at runtime.

Static Yes
Parameters
Param Default Description
$path required The URI path, or an array of paths, to be matched. This is the left side of your routes.php route definition.
$case_sensitive null If false, the given route path will be matched against the URI without regards to case. If true, the match will be case sensitive. If not given, the configured default will be used.
Returns void
Example
// delete the route for 'this/that'
Router::delete('this/that');

// delete the routes for 'this/that' and 'some/other'
Router::delete(array('this/that', 'some/other'));

// force it to be case sensitive
Router::delete('this/that', true);

// delete all routes that point to a module controller
Router::delete('module/controller(:any)');