dplece1882 2011-01-27 12:26
浏览 41
已采纳

Zend Framework - Router_Rewrite链接?

I have a project which uses module directory based on custom routes not the default routes. The system loads each module's routes.ini files and add those routes into a Zend_Controller_Router_Rewrite router.

protected function _initRoutes() {
        $this->bootstrap('FrontController');
        $front = $this->getResource('FrontController');

        $routes = Module_Loader::getInstance()->getRoutes();

        $front->setRouter($routes);
        $front->getRouter()->removeDefaultRoutes();
    }

What i am trying to achieve here is to prepend the language code into the uri but chaining with Router_Rewrite is not possible. I have a route:

[routes]
;Index page
routes.core_index_index.type = "Zend_Controller_Router_Route_Static"
routes.core_index_index.route = "/"
routes.core_index_index.defaults.module = "core"
routes.core_index_index.defaults.controller = "index"
routes.core_index_index.defaults.action = "index"
routes.core_index_index.defaults.frontend = "true"
routes.core_index_index.defaults.langKey = "route_index_page_description"
routes.core_index_index.defaults.localization.enable = "true"

Basically i want the url to look like: http://myhost.com/en/ or http://myhost.com/ which should both direct to this index action of the the homepage controller in the core module.

I can have this appended to the routing rule above as "/([en/]*)" but i don't want the module to care about handling such system-related functionalities.

Is this possible?

Thanks.

  • 写回答

1条回答 默认 最新

  • doubu1970 2011-01-27 13:02
    关注

    You can use the below configuration. It should work for all your controllers:

    [routes]
    ;Index page
    routes.core_index_index.route = ":language/:@controller/:@action/*"
    routes.core_index_index.defaults.controller = "index"
    routes.core_index_index.defaults.action = "index"
    routes.core_index_index.reqs.language= "[a-z]{2}"
    routes.core_index_index.defaults.frontend = "true"
    routes.core_index_index.defaults.langKey = "route_index_page_description"
    routes.core_index_index.defaults.localization.enable = "true"
    

    You need to set up parameter 'language' every time you use this route. To make things easier you can set up default value for 'language' like this:

    Zend_Controller_Front::getInstance()->getRouter()->setGlobalParam('language', 'en');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办