views:

212

answers:

2

Hi,

It is possible to map the extension of a URL, to the format parameter in ZF?

I'd like the default routing to still work, including mapping parameters from the URI, so you'd be able to say:

http://example.com/controller/action/param1/value1/param2/value2.json

Here: $this->_getParam('format') => "json"

And likewise:

http://example.com/module/controller/action/param1/value1/param2/value2.xml

Here: $this->_getParam('format') => "xml"

I've fiddled with the default routes, but i cannot get it to work..

A: 

Hi,

I suppose you could use the action helper ContextSwitch, coupled with a bit of URL rewriting, to rewrite "mycrontroller/myaction.json" to "mycrontoller/myaction/json" or to "mycrontoller/myaction?format=json"

I have not really tested it, and it could probably be written a better way (I'm not an URL rewriting expert ^^ ), but something like this might do the trick :

RewriteRule ^(.*?)\.(.*)$   $1/?format=$2   [L]

What do you think about that ?
Might not be the best way, but I suppose it should work, and be transparent for the use...

Pascal MARTIN
This does not work because `Zend_Controller_Router_Route` match only the original URI (as requested by server) not the rewritten one. --Correct me if I am mistaken.
Petr Peller
+1  A: 

You could create a regex route, ending with something like (\w+)(.(\w+))? and capture the part after the . as the .3 capture. see Zend_Controller_Router_Route_Regex

Justin