2010-03-09 118 views
1

在我的IndexController中,我目前有indexAction(主頁),loginAction和logoutAction。我試圖從URL中刪除「/ index /」以獲取domain.com/login而不是domain.com/index/login。使用Zend_Controller_Router_Route爲IndexController中的操作創建友好的URL

達到此目的的最簡潔方法是什麼?有沒有我們可以使用的RegEx?我從來不想在/ URL中使用/ index /。

我現在的解決方案,我相信可以改進之處在下面。另外,addRoute()中的第一個參數是做什麼的?

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap 
{ 
    protected function _initViewHelpers() 
    { 
    $front = Zend_Controller_Front::getInstance(); 
    $router = $front->getRouter(); 
    $router->addRoute('login', 
     new Zend_Controller_Router_Route('login/*', array(
     'controller' => 'index', 
     'action'  => 'login' 
    )) 
    ); 
    $router->addRoute('logout', 
     new Zend_Controller_Router_Route('logout/*', array(
     'controller' => 'index', 
     'action'  => 'logout' 
    )) 
    ); 
    } 
} 

回答

2

沒有什麼可以改變的,你必須爲每一個動作創建路線。這將允許您在不修改代碼的情況下更改路由默認值(模塊/控制器/操作)。

第一個參數是路徑名稱,你有你的觀點與URL()輔助使用方法:

<a href="<?php echo $this->url(array(), 'login', true); ?>">Login</a> 

更新。您可以使用這樣的路線,如果你只想要一個沒有「指標」,在URL路徑:

$router->addRoute('default', 
    new Zend_Controller_Router_Route(':action/*', array(
    'controller' => 'index', 
)) 
); 
+0

我不能使用,因爲我使用Zend_Navigation管理環節。我正在尋找一種使用RegEx的方法,這樣我只需要使用addRoute()一次。 – jwhat 2010-03-09 20:24:01

+0

我更新了我的答案。順便說一句Zend_Navigation有什麼問題? – 2010-03-10 07:52:18

+0

謝謝!這有效,除了我必須將addRoute()的第一個參數從'default'更改爲'index',否則我的URL會以domain.com/index/controller/login結尾。 Zend_Navigation沒有問題,我的意思是我沒有自己構建超鏈接,我只是在打印$ this-> navigation() - > menu();從我的佈局。 – jwhat 2010-03-10 13:22:48