简体   繁体   中英

Zend Framework - Router_Rewrite chaining?

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.

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');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM