adding a Route to the a Router in Zend Framework

六月ゝ 毕业季﹏ 提交于 2019-12-04 14:53:47

问题


I am using the mod-rewrite router.

I am trying to add a Route to the router that will convert the following url:
baseurl/category/aaa/mycontroller/myaction/param/value

to be:
Controller=mycontroller
action=myaction

--parameters--

category=aaa
param=value

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

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

What am I missing?

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.


回答1:


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



回答2:


$Router=$this->_front->getRouter();
$CategoryRoute = new Zend_Controller_Router_Route('category/:category/:controller/:action/*');
$Router->addRoute('category', $CategoryRoute);

Try adding a start to specify the existence of extra params




回答3:


Please check http://webhkp.wordpress.com/2012/01/01/zend-framework-custom-router/ this would solve your porblem.

its done with an ini file. i like to do it this way




回答4:


You have to specify the defaults when creating the route (see dcaunt's post) OR specify all of the parameters in url view helper (category, controler and action)



来源:https://stackoverflow.com/questions/916143/adding-a-route-to-the-a-router-in-zend-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!