實作以下巢狀路由結構:
/user → 使用者列表/user/:id → 使用者個人頁(如 /user/AlloVince)/user/:id/blog → 該使用者部落格列表/user/:id/blog/:blog_id → 該使用者特定文章Application/Common/Conf/config.php'URL_ROUTER_ON' => true,
'URL_MODEL' => 2, // REWRITE 模式
'URL_HTML_SUFFIX' => '',
'URL_PARAMS_BIND' => true,
'URL_PARAMS_BIND_TYPE' => 0, // 按變數名綁定
'URL_ROUTE_RULES' => [
'/^url$/' => 'Home/User/url',
'/^user$/' => 'Home/User/index',
'/^user\/([\w-]+)$/' => 'Home/User/show?name=:1',
'/^user\/([\w-]+)\/blog$/' => 'Home/Blog/index?name=:1',
'/^user\/([\w-]+)\/blog\/(\d+)$/' => 'Home/Blog/show?name=:1&blog_id=:2',
],
// UserController.class.php
namespace Home\Controller;
use Think\Controller;
class UserController extends Controller {
public function url() {
$name = 'jing'; $blogId = 1;
$urls = [ U('/user'), U("/user/{$name}"), U("/user/{$name}/blog"), U("/user/{$name}/blog/{$blogId}") ];
foreach ($urls as $url) echo "<a href='{$url}'>{$url}</a><br>";
}
public function index() { echo '我是用戶列表^_^'; }
public function show($name) { echo "歡迎你,{$name}"; }
}
// BlogController.class.php
class BlogController extends Controller {
public function index($name) { echo "這是{$name}的博客列表"; }
public function show($blog_id, $name) { echo "{$name}的這篇博客的id為{$blog_id}"; }
}
module/Application/config/module.config.php'router' => [
'routes' => [
'user_list' => [
'type' => 'Segment',
'options' => [
'route' => '/user[/]',
'defaults' => ['controller' => 'User', 'action' => 'index'],
],
'may_terminate' => true,
'child_routes' => [
'user' => [
'type' => 'Segment',
'options' => [
'route' => '[:name][/]',
'constraints' => ['name' => '[\w-]+'],
'defaults' => ['action' => 'show'],
],
'may_terminate' => true,
'child_routes' => [
'blog_list' => [
'type' => 'Segment',
'options' => ['route' => 'blog[/]', 'defaults' => ['controller' => 'Blog', 'action' => 'index']],
'may_terminate' => true,
'child_routes' => [
'blog' => [
'type' => 'Segment',
'options' => [
'route' => '[:blog_id]',
'constraints' => ['blog_id' => '\d+'],
'defaults' => ['action' => 'show'],
],
'may_terminate' => true,
],
],
],
],
],
],
],
],
],
ZF2 採用 AbstractActionController,需手動回傳 ViewModel;參數透過 $this->params()->fromRoute() 取得。**不支援 Action 參數自動綁定**,需手動提取。
Bootstrap.php::_initRoute()$router = Yaf_Dispatcher::getInstance()->getRouter();
$router->addRoute('url', new Yaf_Route_Rewrite('url', ['c'=>'User','a'=>'url']));
$router->addRoute('user_list', new Yaf_Route_Rewrite('user', ['c'=>'User','a'=>'index']));
$router->addRoute('user', new Yaf_Route_Regex('#user/([\w-]+)#', ['c'=>'User','a'=>'show'], [1=>'name']));
$router->addRoute('blog_list', new Yaf_Route_Regex('#user/([\w-]+)/blog#', ['c'=>'Blog','a'=>'index'], [1=>'name']));
$router->addRoute('blog', new Yaf_Route_Regex('#user/([\w-]+)/blog/(\d+)#', ['c'=>'Blog','a'=>'show'], [1=>'name',2=>'blogId']));
$name, $blogId 需與路由映射名一致)。false 可停用視圖渲染。routes.phpRoute::get('/url', ['uses' => 'UserController@getUrl']);
Route::get('/user', ['uses' => 'UserController@getIndex']);
Route::get('/user/{username}', ['uses' => 'UserController@getShow']);
Route::get('/user/{username}/blog', ['as' => 'blog_list', 'uses' => 'BlogController@getIndex']);
Route::get('/user/{username}/blog/{blogId}', ['as' => 'blog', 'uses' => 'BlogController@getShow'])
->where(['blogId' => '[0-9]+']);
// 全域參數模式
Route::pattern('username', '[\w-]+');
class UserController extends Controller {
public function getUrl() {
$name = 'jing'; $blogId = 1;
$urls = [ url('/user'), action('UserController@getShow', [$name]), route('blog_list', [$name]), route('blog', [$name, $blogId]) ];
foreach ($urls as $url) echo "<a href='{$url}'>{$url}</a><br>";
}
public function getIndex() { echo '我是用戶列表^_^'; }
public function getShow($name) { echo "歡迎你,{$name}"; }
}
class BlogController extends Controller {
public function getIndex($name) { echo "這是{$name}的博客列表"; }
public function getShow($name, $blogId) { echo "{$name}的這篇博客的id為{$blogId}"; }
}
route(), action(), url() 輔助函數產生 URL。php artisan route:list 可視化檢視所有路由。| 框架 | 路由定義風格 | 參數綁定 | URL 產生 | 巢狀路由 | 學習曲線 |
|---|---|---|---|---|---|
| ThinkPHP | 陣列正則對應 | 按名稱/順序可選 | U() 函數 | 平鋪正則模擬 | 低 |
| ZF2 | 陣列樹狀結構 | 手動提取 | $this->url()->fromRoute() | 原生子路由 | 高 |
| Yaf | 程式碼註冊 (Rewrite/Regex) | 按名稱綁定 | 無內建(需自行拼裝) | 平鋪 Regex 模擬 | 中 |
| Laravel | 流暢介面 (Fluent) | 按順序綁定 | route()/action()/url() | 隱性巢狀(參數繼承) | 低 |
各框架路由設計反映其核心哲學:ThinkPHP 重約定與簡潔、ZF2 重嚴謹結構與擴充性、Yaf 重效能與 C 擴展特性、Laravel 重開發體驗與表達力。選型時諜考量團隊熟悉度、專案規模與長期維護成本。