Créer des routes dynamiques dans un plugin
Par NairuS le dimanche, février 13 2011, 10:32 - Symfony - Lien permanent
Hello,
Dans le billet précédent j'ai fait référence à un plugin que j'ai développé.
Le sujet d'aujourd'hui porte sur la configuration dynamique des routes de ce dernier à l'instar de sfGuardPlugin.
Pour qu'un plugin avec des modules soit le plus modulaire et flexible possible, il faut pouvoir configurer les modules à activer et les routes à créer dynamiquement selon l'application souhaitée.
Commençons par définir dans le fichier app.yml du plugin les modules à activer selon les applications:
# plugins/nairusPlugin/config/app.yml
# Defines the modules to allow in the differents apps.
all:
nairusPlugin:
modules:
backend:
- back_nairus
- back_nairus_components
- back_nairus_media
frontend: [front_nairus]
jQuery:
load: false
local: /nairusPlugin/js/jquery-1.4.4.min.js
cdn: http://code.jquery.com/jquery-1.4.4.min.js
Ensuite on les charge dans un fichier de config php:
// plugins/nairusPlugin/config/config.php
<?php
if( in_array(sfConfig::get('sf_app'), array('backend', 'frontend')))
{
$enabledModules = sfConfig::get('app_nairusPlugin_modules');
sfConfig::add(array('sf_enabled_modules' => array_merge(
sfConfig::get('sf_enabled_modules'),
$enabledModules[sfConfig::get('sf_app')]
)));
?>
En ce qui concerne les routes on va les créer en php directement dans une classe dédiée:
// plugins/nairusPlugin/routing/NairusPluginRouting.php
class NairusPluginRouting
{
/**
* Dispatchs the route when the routing.load_configuration event is listened
*
* @param sfEvent $event
*/
public static function addRouteForBackNairus(sfEvent $event)
{
$event->getSubject()->prependRoute('back_nairus', new sfDoctrineRouteCollection(array(
'name' => 'back_nairus',
'model' => 'Nairus',
'module' => 'back_nairus',
'prefix_path' => 'back_nairus',
'with_wildcard_routes' => true,
'collection_actions' => array('filter' => 'post', 'batch' => 'post'),
'requirements' => array(),
)));
}
/**
* Dispatchs the route when the routing.load_configuration event is listened
*
* @param sfEvent $event
*/
public static function addRouteForBackNairusComponents(sfEvent $event)
{
$event->getSubject()->prependRoute('back_nairus_components', new sfDoctrineRouteCollection(array(
'name' => 'back_nairus_components',
'model' => 'NairusComponents',
'module' => 'back_nairus_components',
'prefix_path' => 'back_nairus_components',
'with_wildcard_routes' => true,
'collection_actions' => array('filter' => 'post', 'batch' => 'post'),
'requirements' => array(),
)));
}
/**
* Dispatchs the route when the routing.load_configuration event is listened
*
* @param sfEvent $event
*/
public static function addRouteForBackNairusMedia(sfEvent $event)
{
$event->getSubject()->prependRoute('back_nairus_media', new sfDoctrineRouteCollection(array(
'name' => 'back_nairus_media',
'model' => 'NairusMedia',
'module' => 'back_nairus_media',
'prefix_path' => 'back_nairus_media',
'with_wildcard_routes' => true,
'collection_actions' => array('filter' => 'post', 'batch' => 'post'),
'requirements' => array(),
)));
}
/**
* Dispatchs the route when the routing.load_configuration event is listened
*
* @param sfEvent $event
*/
public static function addRouteForFrontNairus(sfEvent $event)
{
$r = $event->getSubject();
$r->prependRoute('front_nairus', new sfRoute(
'/nairus/liste.html',
array('module' => 'front_nairus', 'action' => 'index', 'sf_format' => 'html'),
array(),
array()
));
$r->prependRoute('front_nairus_show', new sfRoute(
'/nairus/:slug.html',
array('module' => 'front_nairus', 'action' => 'show', 'sf_format' => 'html'),
array('slug' => '.*'),
array()
));
}
}
Puis dans notre fichier de config, on les enregistre dans "front controller" de symfony pour les charger via le pattern Observer au moment où les routes de l'application se chargent:
// plugins/nairusPlugin/config/config.php
...
foreach($enabledModules[sfConfig::get('sf_app')] as $module)
{
$this->dispatcher->connect(
'routing.load_configuration',
array(
'NairusPluginRouting',
'addRouteFor' . Doctrine_Inflector::classify($module)
)
);
}
}
...
Maintenant les routes du plugins sont créées dynamiquement au chargement des fichiers de routing selon l'application courante définie dans la config.
Sympa non ?
Have fun !!
A++
NairuS