简体   繁体   English

Zend Framework自定义路线

[英]Zend Framework Custom Routes

I need to create some custom routes in my ZF v.1.12: 我需要在ZF v.1.12中创建一些自定义路由:

protected function _initRouter() {
    $this->bootstrap('FrontController');
    $this->bootstrap('locale');

    $front = Zend_Controller_Front::getInstance ();
    $router = $front->getRouter ();

    $router->addRoute ( 'fastproduct', new Zend_Controller_Router_Route_Regex ( '(.+)\.html', array ('module' => 'default', 'controller' => 'products', 'action' => 'get' ), array (1 => 'q' ), '%s.html' ) );
    $router->addRoute ( 'products', new Zend_Controller_Router_Route_Regex ( 'products/(.+)\.html', array ('module' => 'default', 'controller' => 'products', 'action' => 'get' ), array (1 => 'q' ), 'products/%s.html' ) );
    $router->addRoute ( 'categories', new Zend_Controller_Router_Route_Regex ( 'categories/(.+)\.html', array ('module' => 'default', 'controller' => 'categories', 'action' => 'list' ), array (1 => 'q' ), 'categories/%s.html' ) );
    $router->addRoute ( 'cms', new Zend_Controller_Router_Route_Regex ( 'cms/(.+)\.html', array ('module' => 'default', 'controller' => 'cms', 'action' => 'page' ), array (1 => 'url' ), 'cms/%s.html' ) );
    $router->addRoute ( 'wiki', new Zend_Controller_Router_Route_Regex ( 'wiki/(.+)\.html', array ('module' => 'default', 'controller' => 'wiki', 'action' => 'help' ), array (1 => 'uri' ), 'wiki/%s.html' ) );
    $router->addRoute ( 'tlds', new Zend_Controller_Router_Route_Regex ( 'tlds/(.+)\.html', array ('module' => 'default', 'controller' => 'tlds', 'action' => 'index' ), array (1 => 'uri' ), 'tlds/%s.html' ) );

    return $router;
}

Now If I call these links, works as well: 现在,如果我调用这些链接,也可以正常工作:

# http://www.mydomain.com/productname.html  
# http://www.mydomain.com/products/productname.html
# http://www.mydomain.com/categories/hosting.html
# http://www.mydomain.com/cms/mypage.html
# http://www.mydomain.com/wiki/myhelp.html
# http://www.mydomain.com/tlds/com.html
# http://www.mydomain.com/admin/

but how have I to prepend the language to all these following links? 但是我该如何在以下所有链接之前添加语言?

# http://www.mydomain.com/it/productname.html   
# http://www.mydomain.com/it/products/productname.html
# http://www.mydomain.com/it/categories/hosting.html
# http://www.mydomain.com/it/cms/mypage.html
# http://www.mydomain.com/it/wiki/myhelp.html
# http://www.mydomain.com/it/tlds/com.html
# http://www.mydomain.com/it/admin

thanks for your kindly help. 感谢您的帮助。

Your default route has optional parameters, so route_4 , route_3 and route_2 are unnecessary - get rid of those. 您的默认路由具有可选参数,因此route_4route_3route_2是不必要的-删除这些参数。 You still need the last one, but you are not using route chaining correctly. 您仍然需要最后一个,但是您没有正确使用路由链。 Change that one to: 将其更改为:

$routeLang = new Zend_Controller_Router_Route_Regex('([a-z]{2})', array('module' => 'default', 'lang' => 'en', 'controller' => 'index', 'action' => 'index'), array (1 => 'lang' ), '%s' );
$router->addRoute('route_1', $routeLang);

You might also want to use more descriptive route names for routes you add. 您可能还想对添加的路由使用更具描述性的路由名称。

If it still doesn't work, please update your question to include your updated route config and provide info on how you are checking the matched route params. 如果仍然无法解决问题,请更新您的问题,以包括更新的路由配置,并提供有关如何检查匹配的路由参数的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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