2012-03-27 138 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