18/03/2008
» Symfony 1.0 tip : Registration of plugin routes
Par / by COil,
18/03/2008 / 16:48 :: Tags :: optimization
| plugin
| symfony
[En] Here is a little tip Fabien gave me. I am actually developing a plugin witch has quiet a lot of routes (about 40). Normally you can add the routes with the prependRoute function, like does the sfGuardplugin.
<?php if (sfConfig::get('app_sf_guard_plugin_routes_register', true) && in_array('sfGuardAuth', sfConfig::get('sf_enabled_modules', array()))) { $r = sfRouting::getInstance(); // preprend our routes $r->prependRoute('sf_guard_signin', '/login', array('module' => 'sfGuardAuth', 'action' => 'signin')); $r->prependRoute('sf_guard_signout', '/logout', array('module' => 'sfGuardAuth', 'action' => 'signout')); $r->prependRoute('sf_guard_password', '/request_password', array('module' => 'sfGuardAuth', 'action' => 'password')); } ?>
But for each prependRoute call an array_merge is done on all existing routes.
<?php /** * Adds a new route at the beginning of the current list of routes. * * @see connect */ public function prependRoute($name, $route, $default = array(), $requirements = array()) { $routes = $this->routes; $this->routes = array(); $newroutes = $this->connect($name, $route, $default, $requirements); $this->routes = array_merge($newroutes, $routes); return $this->routes; } ?>
So the tip here is to save all routes, clear them, add the routes of the plugin and then append the saved routes. Witch can be done like this:
<?php // Save and clear all routes $r = sfRouting::getInstance(); $routes = $r->getRoutes(); $r->clearRoutes(); // Plugin home $r->connect('plugin_home', '/my_super_plugin/homepage', array( 'module' => 'my_plugin_module', 'action' => 'my_plugin_action', 'additional_parameter' => 1 )); // Another route $r->connect('plugin_home', '/my_super_plugin/section1', array( 'module' => 'my_plugin_module', 'action' => 'my_plugin_action_section1', 'additional_parameter' => 2 )); // ... other routes // Then merge new routes with the saved one $r->setRoutes($r->getRoutes() + $routes); ?>
That's it.
Of course it is always better to have all routes of the application in the routing.yml but in my case it was not possible.

Indeed we were finishing a new project, an e-commerce website called 








