Routing Callbacks

A routing callback is a class that can intercept certain routing actions and modify parameters or other options. It is notified when


  • A route is generated
  • A route has been matched
  • An attempt was made to match a route but it has not been matched

A routing callback is not notified if maching stopped before there was an attempt to match the route. The routing notifies the callbacks by calling the appropriate methods on the callback - AgaviRoutingCallback::onGenerate() when the route is being generated, AgaviRoutingCallback::onMatched() when the route matched, AgaviRoutingCallback::onNotMatched() in case an attempt to match the route failed. A route may define any number of callbacks that are executed in the order they're defined in, however excessive use of callbacks can slow your routing.

AgaviRoutingCallback::onGenerate()

This method is called when the route is about to be reverse generated into an URL. It can be used to modify the parameters and options passed to the call of AgaviRouting::gen() by the user. This method can be used to turn objects into routing parameters, strip undesired characters from parameters etc. If this method returns false the url-snippet generated by this route is not generated.

AgaviRoutingCallback::onMatched()

This method is called when the regular expression of the route matched. It may inspect and modify any of the parameters extracted from the route or add new parameters. If this method returns false, the route counts as not matched and all parameters modified will be reset. The callback may modify the execution container as well. If the callback returns a response object the resulting response is sent immediatly without any action execution. This behavior can be used for quick redirects.

AgaviRoutingCallback::onNotMatched()

This method is called when the regular expression of the route did not match. The callback may modify the execution container as well. If the callback returns a response object the resulting response is sent immediatly without any action execution. This behavior can be used for quick redirects.

Adding Callbacks to a Route

A list of callbacks may be added to a route by using the <callbacks> tag. The class for the callback must either be included before the routing is executed or more preferably added to the global autoload definition. Any number of parameters may be passed to the callback by adding <ae:parameter> tags as required.

<route name=".post" pattern="^/(post:\d+)(-{title:[-\w]+})?" action="Post">
  <callbacks>
    <callback class="PostRoutingCallback" />
  </callbacks>
  <route name=".show" pattern="^$" action=".Show" />
</route> 

Routing Values

An AgaviRoutingValue is a value object that encapsulates all parameters passed to the routing. It's purpose is to store some meta information about the parameter value, mainly whether it's been encoded or not. All parameters passed to AgaviRoutingCallback::onGenerate() are wrapped in AgaviRoutingValue objects and all parameters changed by this method must be wrapped as well.