The Zend Framework routing process
by Naneau
Because I noticed some people find it difficult to understand the routing process the Zend Framework uses by default, I decided to write a little explanation. It’s really easy once you get the hang of it, even though it may seem complicated at first.
The Zend Framework is based on the Model View Controller principle. This means that for each request you make (every URL you type into your address bar) there should be a matching controller file and action. In that action you could then use models to create a view.
Now, I’ve put up a little tutorial on how to get started with the framework. In it you create a controllers directory and an IndexController.php file. Afterwards you can browse to your site and see a messaged echoed from that file. How does that work?
If you enter a URL the front controller (that’s the one in your bootstrap file) looks for three things. The first is a controller, the second is an action, and the third is parameters. If you enter a url like: http://yoursite.com/foo/bar/baz/abcdefg this would break down to:
a controller: foo
an action: bar
a parameter: baz with value ‘abcdefg’
It would then try to create an object of the class FooController, for which it would look in a file called FooController.php (in your controllers directory). On that object it would try to call a method barAction(). The parameter baz would be set, having a value of ‘abcdefg’.
If you don’t enter an action name, it would look for an indexAction() method and try to call that. If you don’t specify a controller name it would likewise try to look for an IndexController class in an IndexController.php file. That’s the reason I created an IndexController file with an indexAction in my tutorial on setting up the framework. It’s the default controller and default action.
Now, this is the basic strategy for creating controllers and actions. There are more advanced options, like creating modules and using a __call() method. But you should be able to get started now. The beauty of this approach is that you get URLs for your application that are understandable to both computers and humans.
Comments
But what if I had, say, http://mysite.com/news/all/ and I wanted to display a feed at http://mysite.com/news/all/feed/ , how would I go about doing that?
Ah… you’re talking about more complex routes here. I assume you are able to write controllers/actions for both the regular page and the feed? If so, what you can do to achieve such a thing is use the rewrite router and set up routes. You can add a custom route for both /news/all, and /news/all/feed/. Read more about the rewrite router here: http://framework.zend.com/manual/en/zend.controller.router.html
Wow, you are awesome (and Dutch – yay!). I was reading through your other posts (great blog btw) and I just read the third part of your ZF tutorial. It explains everything. Then /me thinks I’d post it here so you don’t have to bother replying and then you’ve already replied! You just made my day
Thank you are lot, now unterstanding the routing process I can try the more complex routing process with specified routes.
What’s about writing a tutorial about Zend_Form, I couldn’t find one in the web ?
thank you very much
Maybe you could help me with a thought I am trying to work out. I am trying to create really clean URLs for pages of a site that will be stored in a database (http://domain/about_us/history). I don’t want to create controllers for all these things or hard code additional routes. I was hoping to detect these pages in a preDispatch function so I could redirect to a page controller that I would create. Maybe I am missing something. Do you have any suggestions on how to do this?
Sorry to post again, but I figured out what I needed to do, or at least a way to get it done. By creating a Front Controller Plugin by extending Zend_Controller_Plugin_Abstract, and overiding the preDispatch method. I can then process the URL and change the controller by calling $this->getRequest()->setControllerName(‘controller’) and $this->getRequest()->setActionName(‘action’);. I hope that someone else may find this to be useful.
Hello Naneau, I am trying to accomplish setting up url scheme just like apple movie trailer site…
http://www.apple.com/trailers/
http://www.apple.com/trailers/universal/
http://www.apple.com/trailers/universal/rolemodels/
I’m confused on how to properly map urls
I’m willing pay for your help….thanks
I think what you’re looking for here is a static route. You can easily add those to your router. http://framework.zend.com/manual/en/zend.controller.router.html#zend.controller.router.routes.static will give you some pointers on how to do that
Hi all,
I’m triying to set routes based on prefixed actions, for example:
An UserController with prefixed with ‘admin_’ string actions (admin_editAction(), admin_listActions()…)
so every time i write /admin/:controller/:action my router redirects me to
Controller->prefixed with ‘admin_’ . $actionname
for the example -> /admin/user/edit/ will be controller user, action admin_editAction
it is possible?
great blog Naneau, thx for your efforts