2012-12-06 55 views
0

在数据库中有给定的文章URL(例如“article1”,“article2”,“article3”)。没有控制器和动作的Zend路由

当我输入www.example.com/article1我想要路由到 控制器:索引, 操作:索引。

我的路线是:

//Bootstrap.php 
public function _initRoute(){  
    $frontController = Zend_Controller_Front::getInstance(); 

    $router = $frontController->getRouter(); 
    $router->addRoute('index', 
     new Zend_Controller_Router_Route('article1', array(
      'controller' => 'index', 
      'action' => 'index' 
     )) 
    ); 
} 

但是当我点击另一个链接(功能)之前,我www.example.com/article1爬不起来。 有没有办法通常为数据库中的所有URL做这种路由?喜欢的东西:

$router->addRoute('index', 
     new Zend_Controller_Router_Route(':article', array(
      'controller' => 'index', 
      'action' => 'index' 
     )) 
    ); 

回答

0

我通常设置ini文件,而不是去的XML路线或“新Zend_Controller_Router_Route的”方式。在我看来,它更容易组织起来。这就是我如何做你正在寻找的东西。我建议对路由进行一些更改,而不是使用http://domain.com/article1的路由,但更像http://domain.com/article/1。这两种方式都是我会在你的情况下做的。

在你routes.ini文件

routes.routename.route = "route" 
routes.routename.defaults.module = en 
routes.routename.defaults.controller = index 
routes.routename.defaults.action = route-name 
routes.routename.defaults.addlparam = "whatevs" 

routes.routename.route = "route2" 
routes.routename.defaults.module = en 
routes.routename.defaults.controller = index 
routes.routename.defaults.action = route-name 
routes.routename.defaults.addlparam = "whatevs2" 

routes.route-with-key.route = "route/:key" 
routes.route-with-key.defaults.module = en 
routes.route-with-key.defaults.controller = index 
routes.route-with-key.defaults.action = route-with-key 

在引导文件

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap 
{ 

#... other init things go here ... 

protected function _initRoutes() { 

    $config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/routes.ini'); 
    $front = Zend_Controller_Front::getInstance(); 
    $router = $front->getRouter(); 
    $router->addConfig($config,'routes'); 
    $front->setRouter($router); 
    return $router; 

    } 

} 

在你的控制器,你可以再做到这一点

class IndexController extends Zend_Controller_Action { 

    public function routeNameAction() { 
     // do your code here. 
     $key = $this->_getParam('addlparam'); 

    } 

    public function routeWithKeyAction() { 

     $key = $this->_getParam('key'); 

     // do your code here. 

    } 
} 
+0

这不是我所期待的。你可以编辑你的文章没有参数“钥匙”?示例网址:www.example.com/new-article,www.example.com/best-article ... www.example.com/ – user1876221

+0

末尾只有一个“参数”,这是第一个块在* .ini部分可以根据需要创建尽可能多的这些“路线”。如果你需要传递额外的参数,你可以在这些块中使用 routes.routename.defaults.addlparam =“你想要什么”; 然后你可以访问这些参数。这样你可以有多个路由指向一个控制器。 –

相关问题