0

我需要在我的 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;
}

现在,如果我调用这些链接,也可以:

# 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/

但是我如何将语言添加到所有这些以下链接中?

# 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

感谢您的帮助。

4

1 回答 1

0

您的默认路由具有可选参数,因此route_4,route_3并且route_2是不必要的 - 摆脱那些。您仍然需要最后一个,但您没有正确使用路由链接。将其更改为:

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

您可能还想为您添加的路线使用更具描述性的路线名称。

如果它仍然不起作用,请更新您的问题以包含您更新的路由配置并提供有关您如何检查匹配的路由参数的信息。

于 2013-09-02T10:05:45.423 回答