The blog of COil : PHP, symfony & Web 2.0

Strangebuzz...?

Réduire au titre / collapse all

18/03/2008

» Symfony tip : Registration of plugin routes

[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.

04/02/2007

» Snippet : Strip comments of propel classes

[En] In Symfony, comments generated by Propel can sometimes be quiet big. With the 0.6.3 version there was no feature to do this. That's why i made this little batch. We could also imagine extend this snippet to remove all the comments off all php files of an application (for production). If you are using 1.0.0 just add in your "config/propel.ini" (1) :


[Fr] Dans Symfony, les commentaires générés par Propel peuvent parfois être assez gros. Avec la version 0.6.3, il n'y a pas de fonctionnalité pour ne pas les créér. C'est pourquoi j'ai fait ce petit batch. On pourrait aussi imaginer d'étendre ce snippet pour supprimer l'ensembles des commentaires des fichiers php d'une application. (en production). Si vous utilisez la 1.0.0, pas besoin de ce batch cette fonctionnalité est désormais incluse, ajouter dans votre "config/propel.ini" :

(1)

 propel.builder.addIncludes = false
 propel.builder.addComments = false