在Zend Framework中将路由添加到路由器 [英] adding a Route to the a Router in Zend Framework
问题描述
我正在使用mod-rewrite路由器。
I am using the mod-rewrite router.
我正在尝试将路由添加到路由器,该路由器将转换以下网址:
baseurl / category / aaa / mycontroller / myaction / param / value
I am trying to add a Route to the router that will convert the following url:
baseurl/category/aaa/mycontroller/myaction/param/value
将会是:
Controller = mycontroller
action = myaction
to be:
Controller=mycontroller
action=myaction
-参数-
类别= aaa
参数=值
category=aaa
param=value
我在引导程序中使用了以下命令(不起作用),_front是frontController
I am using the following (not working) in my bootstrap, _front is the frontController
$Router=$this->_front->getRouter();
$CategoryRoute = new Zend_Controller_Router_Route('category/:category/:controller/:action/*');
$Router->addRoute('category', $CategoryRoute);
使用Zend_View :: url()帮助程序时,我得到的错误是引发的路由器异常(具有或不具有新路线的名称)。
仅当我具有baseurl / category / ...时才引发异常。
The error I get is a thrown router exception when I am using the Zend_View::url() helper (with or without giving it the name of the new route).
The exception is thrown only when I have baseurl/category/....
我错过了什么?
我错过了什么:
由于网址中包含[category],因此使用的路由器是
当我使用url()帮助器时,我没有给[category]赋任何值,因此在url部件->失败中该键没有任何值。
设置为默认值即可使其正常工作。
What I missed:
Since there was [category] in the url, The router that was used is the one defined above.
When I used the url() helper, I didn't give any value in it to the [category] hence there was no value for this key in the url parts->failure.
Giving a default, makes it work.
推荐答案
您应包括solomongaby建议的/*。
You should include the /* as suggested by solomongaby.
如果未提供所有必需的参数(即类别,控制器和操作),则需要指定默认值。
If not supplying all of the required parameters (i.e. category, controller and action), you will need to specify defaults.
您可以按照以下步骤操作:
You can do so as follows:
$Router=$this->_front->getRouter();
$CategoryRoute = new Zend_Controller_Router_Route('category/:category/:controller/:action/*',
array(
'controller' => 'index',
'action' => 'index',
'category' => null
)
);
$Router->addRoute('category', $CategoryRoute);
这篇关于在Zend Framework中将路由添加到路由器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!