Plugins and Routes

By Andy, filed under CakePHP, Tips, Plugins, Routes

Routes are a great feature, but if you try using them with Plugins you might find that you start pulling your hair out. Fear not however, there's always a solution and it's usually not that difficult. If you use routes of the form below, you can map directly to a plugin's page:

$Route->connect('/Something/*', array('controller' => 

'PluginName', 'action' => 'ControllerName', 'Action'));
// Cake 1.2 syntax
Router::connect('/Something/*', 

array('controller' => 'PluginName', 'action' => 'ControllerName', 'Action'));
  1. $Route->connect('/Something/*', array('controller' =>
  2. 'PluginName', 'action' => 'ControllerName', 'Action'));
  3. // Cake 1.2 syntax
  4. Router::connect('/Something/*',
  5. array('controller' => 'PluginName', 'action' => 'ControllerName', 'Action'));
  6.  

Easy when you know how.

One word of warning though, the first parameter in this route has a default which is the controller action. This means that if you create route which expect parameters (as I did in the example) it will work with no parameters, but if any parameters are passed the first one will be understood to be the method name.

I suspect that at some point in the future, it'll be possible to directly specify the plugin in the route, but until then... ;)

«  »

    Comments are now closed, however feel free to send an email with your thoughts