Router クラス
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 |
パラメータ |
パラメータ |
規定値 |
説明 |
$request |
必須 |
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.
|
|
返り値 |
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 |
パラメータ |
パラメータ |
規定値 |
説明 |
$path |
必須 |
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. |
|
返り値 |
void |
例 |
// 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 |
パラメータ |
パラメータ |
規定値 |
説明 |
$name |
null |
Name of the route to load. |
$named_params |
array() |
Array of parameters to be added to the URI created. |
|
返り値 |
bool|array |
例 |
// 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.
Since regexes can't be identified by name, a numeric index is used for each regex in the route,
processed in the order in which they are defined:
// if your route is defined like this:
return array(
'hello/(:name)/(:segment)/(\d{2})' => array('welcome/user', 'name' => 'user'),
);
// this call will return 'http://your_base_url/welcome/user/john/article/16'
echo Router::get('user', array('name' => 'john', 'article', '16'));
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 in its 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 |
パラメータ |
パラメータ |
規定値 |
説明 |
$path |
必須 |
The URI path 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. |
|
返り値 |
void |
例 |
// delete the route for 'this/that'
Router::delete('this/that');
// 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)');
|