1

在数据库中有给定的文章 URL(例如“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'
        ))
    );
4

1 回答 1

1

我通常设置一个 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

在您的引导文件中

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.

    }
}
于 2012-12-07T00:24:35.373 回答