2009-01-05 116 views
1

我正在嘗試將操作映射到基本URL和默認控制器。URL映射的Zend Framework問題

發誓這應該是直截了當的,甚至是一個'開箱'的功能,但如何使用Zend框架將默認控制器中的動作映射到基本網址?我對框架相當陌生,所以我希望我只是錯過了一些明顯的東西。

試圖映射:

domain.com/index/my-page到domain.com/my-page

沒有默認違反任何其他途徑設置的。

我可以使用Zend_Router破解它,但它打破了其他規則,例如/:controller /:action /:module /:controller /:action

note:/ index/my-page是一個示例url - 我需要它動態地爲所有indexController動作工作。

URL映射例子由 'tharkun' 作爲請求 indexController的具有方法的indexAction和contactAction 需要網址

/index 
/index/contact 
/
/contact 

第二控制器的TestController具有方法的indexAction和monkeyAction 需要網址

/test 
/test/index 
/test/monkey 

基本上 - 如果sys不能fi找到VAR的控制器,然後它在默認控制器中尋找VAR的動作

+0

道歉應該使它更清楚,這只是我的示例URL - 我需要它動態地爲所有indexController動作工作,當我添加正則表達式和捕獲是當它開始打破其他默認路由器。 – coffeerings 2009-01-05 14:35:58

回答

2

默認控制器是IndexController。

(默認)映射的工作原理是這樣的:

/ => IndexController::indexAction 
/index => IndexController::indexAction 
/index/foo => indexController::fooAction 
/foo => FooController::indexAction 

所以,添加一個用戶定義的路線是這樣的(會低於默認優先級)

$frontController = Zend_Controller_Front::getInstance(); 
$router = $frontController->getRouter(); 
$route = new Zend_Controller_Router_Route(
    '/:action', 
    array(
     'controller' => 'index' 
    ) 
); 
$router->addRoute('user', $route); 

這並沒有打破我的使用默認路由。

編輯:正如coffeerings在評論中所說,這將打破非默認控制器的indexAction。

+0

差不多:)它適用於所有其他控制器的默認操作。所以indexController方法/,/ index和/ testme可以工作,但是在testController中只有/ test/index和/ test/testme工作,默認/ test/action失敗。 – coffeerings 2009-01-05 15:16:24