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'));
|