Page 1 of 1
Nice URLs with Zend Application
Posted: Sat Apr 17, 2010 3:35 pm
by onion2k
How do you do nice URLs, eg domain.com/user/chris/profile/comments with Zend Application? I can't find any information about using routing with the Zend_Application stuff.
EDIT: By default Zend Application lets you do something like domain.com/user/profile/name/chris/page/comments, where the URL is like name=value pairs, I know that. But that's horrible. I want to lose the name= bits, and just end up with a method in my Controller with an array of values from the URL.
Re: Nice URLs with Zend Application
Posted: Sat Apr 17, 2010 4:32 pm
by Eran
Disclaimer - I don't use Zend_Application at all, I write my own bootstrap files. Zend_Application is an API for bootstrapping, but obviously it is limited by what the API can offer.
Looking at the documentation though, you can retrieve the front controller from the bootstrap. You can then set a custom router configured with your own routes in addition to the defaults.
Code: Select all
//From inside a bootstrap class
$this -> bootstrap('FrontController');
$front = $this -> getResource('FrontController');
//Set up your router
$router = new Zend_Controller_Router_Rewrite();
... custom routing logic ...
$front -> setRouter($router);
For example, the route you suggested:
Code: Select all
$router -> addRoute('userprofile',new Zend_Controller_Router_Route(
'user/:username/profile/:action',
array(
'controller' => 'user'
)
));
You can read more about the different types of routes in the router documentation -
http://framework.zend.com/manual/en/zen ... outer.html
Re: Nice URLs with Zend Application
Posted: Mon Apr 19, 2010 4:27 am
by onion2k
Cheers. Got that working.
One question though.. is that the best way of doing it? In a large site there are going to be hundreds of routes, and initialising all of them just to check which Controller the script should use feels wrong. At least if they're set up in Apache they're cached. I guess it's an issue with all PHP based MVC approaches, unless there's a way of doing it that I've missed..
Re: Nice URLs with Zend Application
Posted: Mon Apr 19, 2010 4:41 am
by Eran
I usually have a separate file containing the routes and I include it when I set those up (the routes are all in an array). Those are custom routes though, I still use the basic /controller/action/key/value for about 90% of most of the site URLs, so even on very large sites there are only about a couple dozen custom routes. If you're clever in the way you set those up as well you can avoid redundant routes. You can also use the more advanced route types to cover more cases.