2

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.

Jimit
  • 2,201
  • 7
  • 32
  • 66
  • You have asked 35 Questions and accepted only 7 Answers....! Please accept answers that you found helpful..... – Pushpendra May 10 '11 at 13:54

2 Answers2

3

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'))
);
Poelinca Dorin
  • 9,577
  • 2
  • 39
  • 43
  • @Jimit: add the code at bootstrap ( Bootstrap.php file ), or make a plugin with a "hook" on routeStartup . Follow the link provided and it will explain everithing ( if not then there are tons of tutorials on how zend routing works and how to achive certan things ) . – Poelinca Dorin Mar 14 '11 at 12:44
2

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.

Marcin
  • 215,873
  • 14
  • 235
  • 294