Custom route class

In symfony 1.4, you can define your own route class, where you override URL generation using custom logic, for example:

custom:
  class: sfDoctrineRouteCollection
  options:
    model:                Custom
    prefix_path:          /custom/category/:category_id
    column:               id
    route_class:          CustomDoctrineRoute

class CustomDoctrineRoute extends sfDoctrineRoute
{
  public function generate($params, $context = array(), $absolute = false)
  {
    if (!isset($params['category_id'])) {
      $params['category_id'] = sfContext::getInstance()->getRequest()->getParameter('category_id');
    }

    return parent::generate($params, $context, $absolute);
  }

}

This allows you to write url_for('custom_show', array('id'=> $object['id']))and not worry about context-sensitive parameters (category_id).

How do you approach this symfony2?

+5
source share
1 answer

I can come up with two approaches to this. The first and simplest is to extend the Router class with your own and tell symfony to use your class in your .yml or config.yml options:

parameters:
    router.class: Company\CoreBundle\Routing\MyCustomRouter

(: ) , , . , BeSimpleI18nRoutingBundle, , .

, CompilerPass, router . RouterInterface . ( ).

+3

All Articles