Nice URLs with Zend Application

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Nice URLs with Zend Application

Post 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.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Nice URLs with Zend Application

Post 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
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Nice URLs with Zend Application

Post 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..
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Nice URLs with Zend Application

Post 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.
Post Reply