I have found a way to work around the issue that the default parameters get left out of the url that the url()
helper configures. When zf2 develops the url from the route statements, it drops out everything within the []
brackets that match the default parameters. Therefore,
<a href="<?php echo $this->url('album',
array('action'=>'index', 'arg1' => 'jazz'));?>">Jazz</a>
constructs <a href="/album/jazz">Jazz</a>
instead <a href="/album/index/jazz">Jazz</a>
of when the route statement includes 'route' => '/album[/:action][/:arg1]',
as I presented in the question. However, if I breakout the /
character from the parameter(s) and give it it's own[]
brackets, the url()
helper only drops the :action
part of the url. In other words, if I replace
'route' => '/album[/:action][/:arg1]',
with
'route' => '/album[/][:action][/][:arg1]',
then
<a href="<?php echo $this->url('album',
array('action'=>'index', 'arg1' => 'jazz'));?>">Jazz</a>
constructs <a href="/album//jazz ">Jazz</a>
, which points to the page it should.
I also got a lot out of timdev's answer, "It's worth it to take a step back and decide how you want your url scheme to work, and then figure out how to implement it." If I want to add a whole lot of bells and whistles to how I want to query, filter and sort the data, theindex
action (which manages multiple albums) is going to want to take a completely different set of parameters than the add
, edit
and delete
actions (which each only manage a single album at a time.) So I set up two routes:
'router' => array(
'routes' => array(
'albums' => array(
...
'route' => '/albums[/][:action] [/][:arg1] [/][:arg2] [/][:arg3] ... ',
...
),
'album' => array(
...
'route' => '/album[/][:action][/][:id]',
...
),
),
),