For some time now there have been lots of discussions regarding the use of frameworks. Some say it causes too much overhead, is a deal breaker from a performance point of view and forces you to adapt your project to the standards of the framework. Others defend the DRY (Do Not Repeat Yourself) principles and the fact that the source code of influential frameworks is kept up to date by professionals.
I consider myself an advocate of frameworks because they decrease the time to deliver considerably. Nevertheless I also understand the overhead issues. That’s why in this article, which is part of a series of articles, I will try to prove that Zend Framework is an actual glue framework. This means you can just use a piece of the framework’s functionality you like and integrate it in your own logic.
Concept
I’d like to kick off with a post that shows how you can use Zend_Controller_Router_Rewrite without being forced to use the entire MVC stack in which it fits.
People who aren’t familiar with the MVC concepts are better off reading the Wikipedia definition first. Another interesting piece of reading is the Zend Framework manual extract on MVC and more particularly on the controller.
The router isn’t an indispensible part of your MVC setup, but it is an important utility of the front controller. The router translates the URL into a set of key values the front controller uses:
- The controller name
- The action name
- The extra parameters to use
Based on this information the front controller calls the apropriate controller action in which the model data is passed to a view.
It also works the other way around: in your view you can setup a hyperlink that isn’t a fixed URL, but a reference to a named route. This named route is stored in your routes.ini file and is then converted to a full URL. This is very convenient when you don’t want to depend on one single naming convention for your URL paths. If you use the default setup which is managed by the front controller, a set of default routes is predefined.
Have I mentioned the router also has a bling bling factor? Well yeah … it allows you to use those clean URL’s everyone likes without having to depend on your .htaccess rewrite rules. There’s only one catch-all rule in the .htaccess. The complete URL rewriting foo happens in Zend Framework itself.
Tearing it apart
As mentioned: there can be some performance factors that do not allow you to use the full MVC stack. In case you want to use the router separately there isn’t that much information available. The way I did it was by analyzing the source code to know how Zend Framework interacts with its router. Our proof of concept consists out of 2 opertions:
- Call a URL by using a named route and let the script convert it to the parameters needed by MVC
- Compose a new URL by assembling a set of MVC parameters
Assemble your toolbox
To make it as lightweight as possible we only use the essentials. The following classes are needed:
- Zend_Loader: to autoload all other dependencies
- Zend_Controller_Router_Rewrite: the actual router
- Zend_Config_Ini: to store your named routes
- Zend_Controller_Request_Http: to retrieve your URL
Workflow
- Enable your autoloader that allows dependencies to be loaded automatically
- Create a new router
- Create a new configuration object based on the routes.ini file
- Fetch the HTTP data of the current URL into a request object
- Add the custom routes from routes.ini by passing the configuration object to the router
- Pass the current HTTP request object to the router which will extract all necessary information
- Use the router’s getter methods to retrieve the MVC parameters
Example
Now we know what to do, so it is time to throw some source code. As you can see the example consists of 3 files:
- The application itself (represented by index.php)
- The route configuration (represented by routes.ini)
- The Apache configuration file that performs the URL rewriting (represented by .htaccess)
Your MVC application: index.php
We start with the first task: converting the URL into a set of logical parameters.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <?php //Setup loader require_once 'Zend/Loader.php'; Zend_Loader::registerAutoload(); //Init classes $router = new Zend_Controller_Router_Rewrite(); $config = new Zend_Config_Ini('routes.ini'); $request = new Zend_Controller_Request_Http(); //Convert current URL to MVC items $router->addConfig($config,'routes'); $request = $router->route($request); //Print route name and MVC items echo '<h1>Retrieve action/controller/params from current URL</h1>'; echo 'Current URL: http://'.$_SERVER['HTTP_HOST'].$request->getRequestUri().'<br />'; echo 'Current Route:'.$router->getCurrentRouteName().'<br />'; foreach($request->getParams() as $paramName=>$paramValue) echo ucfirst($paramName).': '.$paramValue.'<br />'; |
Add some spacing to your output
17 18 | //Space echo '<hr />'; |
We continue with our second task: composing a new URL
19 20 21 22 23 24 25 26 27 28 | //Assemble other URL from MVC items $route = $router->getRoute('customRoute'); $uri = $route->assemble(array('id'=>'5','name'=>'bla')); $url = 'http://'.$_SERVER['HTTP_HOST'].'/'.$uri; //Print new url and MVC items echo '<h1>Build new URL from route and params</h1>'; echo 'Route: customRoute<br />'; echo 'Id: 5<br />'; echo 'Name: bla<br />'; echo 'URL: <a href="'.$url.'">'.$url.'</a><br />'; |
Define your custom routes: routes.ini
1 2 3 4 5 | routes.customRoute.route = my/custom/route/:name/:id/* routes.customRoute.defaults.controller = myController routes.customRoute.defaults.action = myAction routes.customRoute.defaults.id = 1 routes.customRoute.defaults.name = name |
Define the rewrite rules: .htaccess
1 2 3 4 | <IfModule mod_rewrite.c> RewriteEngine on RewriteRule !\.(js|ico|gif|jpg|png|css)$ index.php </IfModule> |
Hey Thys,
This is great stuff! I’m already looking forward to the next posts in these series. Everybody is stepping up to give more meaningful examples for Zend Framework, making it a whole less complicated for others to enjoy the PHPun and power of Zend Framework. Job well done.
Btw, you should go for your ZFCE!!!
Michelangelo
Great! Thank you!
I always wanted to write in my blog something like that. Can I take part of your post to my blog?
Of course, I will add backlink?
Regards, Your Reader
No problem what so ever. I appreciate the backlink.