Need Help in zend framework URL Rewrite

。_饼干妹妹 提交于 2019-12-10 10:54:48

问题


I need help in URL rewrite in zend framework. If I print below URL :

echo $this->url(array('controller'=>'guestbook','action'=>'edit','id'=>$entry->id), null, TRUE);

It will generate url like : http://localhost/guestbook/public/index.php/guestbook/edit/id/1

But How can I generate url like : http://localhost/guestbook/public/index.php/guestbook/edit/1 in zend framework?

I don't want 'id' in URL. Please Help.


回答1:


To make it work you need to define a custom route, called e.g. guestbook, and make url helper to use it for your particular url.

For example, in your application.ini you can define it as follows:

resources.router.routes.guestbook.route = '/guestbook/edit/:id'
resources.router.routes.guestbook.defaults.controller = user
resources.router.routes.guestbook.defaults.action = edit
resources.router.routes.guestbook.defaults.id = 
resources.router.routes.guestbook.reqs.id = "\d+" 

Then, you use the url helper in the following way:

echo $this->url(array('controller'=>'user','action'=>'edit','id'=>2), 'guestbook', TRUE);

Hope that helps.




回答2:


Zend Controller Router will help you achive this .

The easyest way to get started would be at bootstrap add the following ( not realy tested but it should work with minimal debuging, see the link provided as it explains a ton more, use this code just to get started on understanding how routes work ) :

$router = Zend_Controller_Front::getInstance()->getRouter();
$router->addRoute(
    'guestbook_edit',
    new Zend_Controller_Router_Route('guestbook/edit/:id',
                                     array('controller' => 'guestbook',
                                           'action' => 'edit'))
);


来源:https://stackoverflow.com/questions/5298386/need-help-in-zend-framework-url-rewrite

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