Zend Framework Url View Helper adds "id" by default

Niels Bom picture Niels Bom · Nov 18, 2010 · Viewed 11.6k times · Source

I'm creating a simple CRUD for adding links to a category. Each category has an id. I have a view that lists all the links for a certain category. In that view I have a link to the add-form which is:

http://example.com/link/add/categoryId/3

I currently build that link in the view using the following syntax.

<?php echo $this->baseUrl();?>/link/add/categoryId/<?php echo $this->category['id']; ?>

I think this can be done cleaner by using the Url View Helper.

<?php echo $this->url(array('controller'=>'link','action'=>'add','categoryId'=>$this->category['id'])); ?>

But that gives me the following url

http://example.com/link/add/id/3/categoryId/3

..which has an extra "id/3". I read but did not fully understand the code of the Url View Helper. How come there's an extra id/3 in there?

Thanks!

@Fge gave the correct answer, below is my updated complete syntax.

echo $this->url(array('controller'=>'link','action'=>'add','categoryId'=>$this->category['id']),null,true);

Answer

Fge picture Fge · Nov 18, 2010

By default the Url ViewHelper merges and overrides the given parameters with the current request parameters. Like in your case the id-parameter. If you want to reset all parameters you have to use the 3rd parameter of the view-helper: 'reset':

$this->url(array(), 'route'( = null to use the default), true);

This will force the viewhelper not to use the current request as "fallback" for not set parameters. The default behaviour is especially usefull if you only want to change one or two parameters of the current request (like the action) but don't want to set all parameters (or maybe you don't even know them all).