Bootstrapping the Application: the Standard Router Resource Plugin
Sunday, September 27, 2009

The Zend Framework uses the Zend_Controller_Router_Rewrite class to decompose requests into module/controller/action/parameters, which are then despatched to the appropriate class for handling. It is possible to attach some custom handling to the router to allow for special handling of certain requests. For instance, you may want to allow Google to retrieve your sitemap via a request for http://www.example.com/sitemap.xml. The router allows this to be converted into a request for a particular combination of module/controller/action/parameters, which can then be handled in the usual way. E.g. the above request might be treated as equivalent to http://www.example.com/default/sitemap/index.

Zend_Application_Resource_Router, the standard router resource plugin, presents a simple mechanism for adding routes to the router. For each route you wish to add, you need to add a block like this to the configuration file:

resources.router.routes.sitemap.route = "sitemap.xml"
resources.router.routes.sitemap.defaults.controller = "sitemap"

The sub-element after routes (here sitemap) is set to a unique name for the route. There are many parameters that can be set; here we set two:

  • route: This represents the actual request, which is for sitemap.xml.
  • defaults: This represents what the request is converted to; here we only specify the controller, but it is also possible to specify other things such as the module, action or parameters.

Now any request for sitemap.xml will be redirected to the index action of the sitemap controller.

We will look at how the request is actually handled in a later post.

Posted by James at 2:03 pm   0 comments

Leave a Reply