PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Using Zend_Config with the RewriteRouter

Sometimes it is more convenient to update a configuration file with new routes than to change the code. This is possible via the addConfig() method. Basically, you create a Zend_Config-compatible configuration, and in your code read it in and pass it to the RewriteRouter.

As an example, consider the following INI file:

<?php
[production]
routes.archive.route "archive/:year/*"
routes.archive.defaults.controller archive
routes
.archive.defaults.action show
routes
.archive.defaults.year 2000
routes
.archive.reqs.year "\d+"

routes.news.type "Zend_Controller_Router_Route_Static"
routes.news.route "news"
routes.news.defaults.controller "news"
routes.news.defaults.action "list"

routes.archive.type "Zend_Controller_Router_Route_Regex"
routes.archive.route "archive/(\d+)"
routes.archive.defaults.controller "archive"
routes.archive.defaults.action "show"
routes.archive.map.1 "year"
; OR: routes.archive.map.year 1

The above INI file can then be read into a Zend_Config object as follows:

<?php
$config 
= new Zend_Config_Ini('/path/to/config.ini''production');
$router = new Zend_Controller_Router_Rewrite();
$router->addConfig($config'routes');

In the above example, we tell the router to use the 'routes' section of the INI file to use for its routes. Each first-level key under that section will be used to define a route name; the above example defines the routes 'archive' and 'news'. Each route then requires, at minimum, a 'route' entry and one or more 'defaults' entries; optionally one or more 'reqs' (short for 'required') may be provided. All told, these correspond to the three arguments provided to a Zend_Controller_Router_Route_Interface object. An option key, 'type', can be used to specify the route class type to use for that particular route; by default, it uses Zend_Controller_Router_Route. In the example above, the 'news' route is defined to use Zend_Controller_Router_Route_Static.

Zend Framework