我建議你到一些判別值添加到您的網址,以避免碰撞規則。 在這個例子中,我將顯示所有'用戶'相關的網址都在前綴'用戶'之下,所以我們可以有用戶/登錄,用戶/註銷,用戶/註冊,用戶/更改密碼等(btw同樣的邏輯用於zfcuser模塊)。
我們將製作一個像/ user/profile /:user這樣的網址,其中:user部分是網址的動態部分。
我假設你正在使用的應用模塊上,並且您已經創建並註冊了一個UserController的(如果名稱不同,下面的代碼需要改變)
'router' => array(
'routes' => array(
// other routes configs here
'user' => array(
'type' => 'Literal',
'priority' => 1000,
'options' => array(
'route' => '/user',
'defaults' => array(
'__NAMESPACE__' => 'Application\Controller',
'controller' => 'User',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'profile' => array(
'type' => 'Segment',
'options' => array(
'route' => '/profile/:user',
'defaults' => array(
'action' => 'profile',
),
),
),
),
),
使用這條路線配置您的應用程序將UserController的迴應::當url/user被調用時的索引。 當/ user/profile/someuser被調用時,將使用UserController :: profile進行響應。
請注意,someuser部分是強制性的,因爲我們在規則中這樣說。爲了使它不是強制性的,我們應該這樣寫規則遵循
'route' => '/profile/[:user]',
在UserController中的配置文件操作中可以採取:用戶價值一樣遵循
$this->params()->fromRoute('user')
這僅僅是一個解決方案,您可以之一採用,比如你可以這樣寫沒有子路由您的規則,就像跟着
'user' => array(
'type' => 'Segment',
'options' => array(
'route' => '/user/profile/:user',
'defaults' => array(
'__NAMESPACE__' => 'Application\Controller',
'controller' => 'User',
'action' => 'profile',
),
),
),
它是由你來選擇適合您需求的解決方案。 我建議你也閱讀ZF2 official routing documentation。
請閱讀我的快速入門重寫:[第05章 - 理解路由](https:// github。com/manuakasam/ZF2_3_QuickStart_Rewrite/pull/12/files) – Sam