範例 |
// 如果你的路由是像这樣定義:
return array(
'_root_' => 'welcome/index', // 預設路由
'_404_' => 'welcome/404', // 主要的 404 路由
'hello(/:name)?' => array('welcome/hello', 'name' => 'hello'),
);
// 此呼叫将回傳 'http://your_base_url/welcome/hello'
echo Router::get('hello');
你也可以这樣做,如果你的路由包含一个具名參数、正規表達式、或兩者的結合。
// 如果你的路由是像这樣定義:
return array(
'thread/(?P<thread_id>\d+?)/post' => array('post', 'name' => 'post'),
);
// 这些会回傳 'thread/1/post':
echo Router::get('post', array('thread_id' => 1));
echo Router::get('post', array('$1' => 1));
echo Router::get('post', array(1));
// 如果你的路由是像这樣定義:
return array(
'country/(?P<country>\d+?)/state/(?P<state>\d+?)/location' => array('location', 'name' => 'location'),
);
// 这些会回傳 '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'));
請注意,如果你的路由包含一个傳统正規表達式和一个具名參数,或一个简潔的正規表達式,
它們将被一起取代,这可能導致意想不到的結果。
// 如果你的路由是像这樣定義:
return array(
'hello/(:name)(/:segment)' => array('welcome/user', 'name' => 'user'),
);
// 請注意 "(/:segment)" 将完全地被取代为 "article",所以
// 此呼叫将回傳 'http://your_base_url/welcome/user/johnarticle' !
echo Router::get('user', array('name' => 'john', 'article'));
|