Need Help in zend framework URL Rewrite

2019-05-27 03:17发布

问题:

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