没有控制器和动作的Zend路由

时间:2012-12-06 18:00:25

标签: php zend-framework url routing url-routing

数据库中有文章网址(例如“article1”,“article2”,“article3”)。

当我输入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'
        ))
    );

1 个答案:

答案 0 :(得分:0)

我通常设置一个ini文件,而不是使用xml路由或“new 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

在你的bootstrap文件中

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.

    }
}