Wednesday, January 4, 2012

Simple Zend Router example

The default routing setup for Zend is ':module/:controller/:action/*'

The following example will create a route that will only be triggered if the first directory after the public directory (normally used to specify the controller) contains an underscore.

Create a new ini file named route.ini and save it where you save your ini files:

routes.popular.route = :catid/:articleid
routes.popular.defaults.controller = index
routes.popular.defaults.action = category
routes.popular.defaults.catid = 52
routes.popular.defaults.articleid = 1
 
routes.popular.reqs.catid = \w+_\d+
 
 
Now add a method to your bootstrap class:

public function _initRouter()
{ 
 $frontController = Zend_Controller_Front::getInstance();
 $config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/route.ini');
 $router = $frontController->getRouter();
 $router->addConfig($config,'routes');     
}
 
Done.

Url's that contain fit the defined regex pattern (\w+_\d+) will be handled by the categoryAction in the indexControler.
Other url's will be handled normally by the defaut routing system.
 

 

No comments:

Post a Comment