views:

105

answers:

2

Rather than using controller/action/key1/value1/key2/value2 as my URL, I'd like to use controller/action/value1/value2. I think I could do this by defining a custom route in my Bootstrap class, but I want my entire application to behave this way, so adding a custom route for each action is out of the question.

Is this possible? If so, how would I then access valueN? I'd like to be able to define the parameters in my action method's signature. e.x.:

 // PostsController.php

 public function view($postID) {
      echo 'post ID: ' . $postID;
 }

I'm using Zend Framework 1.9.3

Thanks!

+2  A: 

Hi Arms,

While I don't think it's possible with the current router to allow N values (a fixed number would work) you could write a custom router that would do it for you.

I would question this approach, however, and suggest that actually listing all of your routes won't take long and will be easier in the long run. A route designed as you've suggested would mean that either your named parameters are always in the same order, i.e.

/controller/action/id/title/colour

or that they are almost anonymous

/controller/action/value1/value2/value3

With code like

$this->getRequest()->getParam('value2'); //fairly meaningless
David Caunt
'value2' etc. is indeed quite meaningless. You should be able to get away with a bunch of typical routes, like :action/:id, :action/:type/:mode or whatever are specific to your app. No reason to define routes for every action if they take in similar params.
Jani Hartikainen
@Jani Hartikainen, how would I reuse routes? Would I simply assign $r1 = :action/:id, $r2 = :action/:type/:mode and then pass them to new Zend_Controller_Router_Route instances?
Arms
+1  A: 

Does it have to be N or can you say some finite value? For instance can you imagine that you'll never need more than say 5 params? If so you can set up a route:

 /:controller/:action/:param0/:param1/:param2/:param3/:param4

Which will work even if you don't specify all 5 params for every action. If you ever need 6 somewhere else you can just add another /:paramN onto the route.

Another solution I've worked with before is to write a plugin which parses the REQUEST_URI and puts all the extra params in the request object in the dispatchLoopStartup() method. I like the first method better as it makes it more obvious where the params are coming from.

smack0007