2012-03-27 81 views
0

我正在構建一個Zend Framework 1.11.11應用程序,並且想要使路由和內容數據庫驅動。Zend Framework - 路由器 - 創建別名

我已經寫了FrontController插件,從數據庫檢索的「路徑」,並創建在路由器爲每一個的條目,與相關聯的控制器和動作。

不過,我希望能夠使用「別名」 - 這就像一個正常的URL,而是一個別名的URL。

例如,如果我創建以下:

// Create the Zend Route 
$entry = new Zend_Controller_Router_Route_Static(
    $route->getUrl(), // The string/url to match 
    array('controller' => $route->getControllers()->getName(), 
      'action' => $route->getActions()->getName()) 
);      
// Add the route to the router 
$router->addRoute($route->getUrl(), $entry); 

然後,對於/about/例如可以轉到該staticController,的indexAction的路由。

然而,什麼是爲我創造這條路線的別名的最佳方式?所以如果我去/abt/它會呈現相同的控制器和操作?

對我來說沒有任何意義重建,因爲我將要使用的頁面「標識符」來然後從數據庫的頁面加載內容的路線相同的路線...

回答

1

可以擴展靜態路由器:

class My_Route_ArrayStatic extends Zend_Controller_Router_Route_Static 
{ 
    protected $_routes = array(); 

    /** 
    * Prepares the array of routes for mapping 
    * first route in array will become primary, all others 
    * aliases 
    * 
    * @param array $routes array of routes 
    * @param array $defaults 
    */ 
    public function __construct(array $routes, $defaults = array()) 
    { 
     $this->_routes = $routes; 
     $route = reset($routes); 
     parent::__construct($route, $defaults); 
    } 

    /** 
    * Matches a user submitted path with a previously specified array of routes 
    * 
    * @param string $path 
    * @param boolean $partial 
    * @return array|false 
    */ 
    public function match($path, $partial = false) 
    { 
     $return = false; 

     foreach ($this->_routes as $route) { 
      $this->setRoute($route); 
      $success = parent::match($path, $partial); 
      if (false !== $success) { 
       $return = $success; 
       break; 
      } 
     } 

     $this->setRoute(reset($this->_routes)); 
     return $return; 
    } 

    public function setRoute($route) 
    { 
     $this->_route = trim($route, '/'); 
    } 
} 

,並添加新的路由器是這樣的:

$r = My_Route_ArrayStatic(array('about', 'abt'), $defaults); 
+0

感謝您的回覆 - 請你能不能帶註釋更新呢? – Sjwdavies 2012-03-28 08:35:39

+0

我已經提出了一些額外的意見,基本上它只是包裝匹配()方法,它允許匹配$路徑對URL數組 – 2012-03-28 09:42:42

+0

謝謝 - 這似乎達到我需要的結果。如果你能更詳細地解釋你的答案,我將不勝感激。 – Sjwdavies 2012-03-28 14:47:12