Tuesday, April 20, 2010

Custom rules for zend standard rewrite router

In this post, an overview of Zend routing [official doc] and how set custom rule for routing (tested on Zend v1.10).

Routing is basically how zend associate URLs tothe controllers, actions and parameters of the application (similar to apache mod_rewrite but PHP based therefore much more powerful).

Default routing

As it is known, the default Zend routing uses the following schema:

[controller]/[action]/[var1name]/[var1value]/ [var2name]/[var3value]/ ... /[varNname]/[varNvalue]

If the controller or action are not specified, the ones with name “index” are used for routing.

How to set rules

First of all, all the rules have to be attached to the router, that can be returned by using getRouter() on the frontController object (obtainable with singleton: Zend_Controller_Front::getInstance()).

The router is an object of type Zend_Controller_Router_Rewrite, and contains the method addRoute($name, Zend_Controller_Router_Route_Interface $route)

So, we need to attach object of classes that implements Zend_Controller_Router_Route_Interface.

Static routing [zend manual]

The simplest is used for static for rules without variables inside:


$route = new Zend_Controller_Router_Route_Static('login', array('controller' => 'auth', 'action' => 'login') );

After this rule is attached, the URL /login will be the same as /auth/login

Standard routing (with optional default and requirements on parameters) [official doc]


$route = new Zend_Controller_Router_Route('archivenews/:year',
array('controller' => 'news', 'action' => 'archive', 'year' => ‘2010’ /*default value*/ ),
array('year'=>'\w{4}') /*requirement on parameter*/
);
  • The string 'archivenews/:year' is the route. The placeholders starting with semicolon (just “:year” in this example) are variables ( that will replaced with the real values in the URL).
  • The URL /archivenews/2008 will call the controller news, action archive, and will set the parameter ‘year’ to 2008 (obtainable with $this->_getParam('year') inside the controller).
  • The URL /archivenews will call the same controller and action, with ‘year’ to 2010 (default).
  • Validation: The URL /archivenews/20008 will not match any rule, as the validation fails. If the validation (third argument) is removed, the same URL will call the same controller and action with year set to ‘20008’
  • Default value: If the default value is not set (item ‘year’ not present in the second argument), the rule /archivenews will not match

RegEx routing [main]

It supports regular expression inside the route instead of passing as a third argument (slightly faster than the previous one).


$route = new Zend_Controller_Router_Route_Regex('archive-news/(\d{4})',
array('controller' => 'news', 'action' => 'archive')
);

With this rule we’ve defined the same rule as before, with no default values.

As you probably have noticed, the rule does not contain the name of the variable (:year). To get it, use $this->_getParam(1) as “1” is the ordinal number of the placeholder (regular expression) in the route.

Attaching rules to the router

As already said, the rules has to be attached to the router


$ctrl = Zend_Controller_Front::getInstance();
$router = $ctrl->getRouter();
// use $route set above
$router->addRoute('routeName', $route); /*note: do not use the same route name for different rules or they will be overwritten and ignored*/


Post moved here:
Custom rules zend standard rewrite router

No comments:

Post a Comment

 

PHP and tips|PHP