【问题标题】:Zend routing without controller and actionZend 路由没有控制器和动作
【发布时间】:2012-11-24 20:09:36
【问题描述】:

数据库中有给定的文章 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'
        ))
    );

【问题讨论】:

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


    【解决方案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.
    
        }
    }
    

    【讨论】:

    • 这不是我要找的。您可以在没有参数“key”的情况下编辑您的帖子吗?示例 URL:www.example.com/new-article、www.example.com/best-article ... www.example.com/ 的末尾只有一个“参数”
    • 这就是 *.ini 部分中的第一个块,您可以根据需要创建任意数量的这些“路由”。如果您需要传递其他参数,您也可以在这些块中传递 routes.routename.defaults.addlparam = "whatever you want";然后您也可以访问这些参数。这样你就可以有多个路由指向一个控制器。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多