1

My Silex routes are defined in a routing.yml config file.

In my php code I would like to add some new routes dynamically and I want these routes to have a higher priority than the routes defined in routing.yml.

Currently I'm adding my routes as in the following example, but they are added to the bottom, i.e. with the lowest priority.

    $this->app->match('/page/{slug}', array($this, 'record'))
        ->bind('extrapages')
        ->method('GET|POST');

This route for example never gets matched because there is a route in routing.yml that matches the following path:

path: /{contenttypeslug}/{slug}

How do add my new routes above the existing routes?

As an aside I'm using the Bolt CMS, which is built on Silex, and trying to add these new routes in a bolt extension. As this question is about Silex routing the fact that I'm using Bolt shouldn't make a great deal of difference.

matthew
  • 2,156
  • 5
  • 22
  • 38

1 Answers1

1

It's not a very clean solution but whenever I've overruled an extension's route in my routing.yml, I just add the extension's route to routing.yml again, pointing it to my extension code. If you put it near the top, it will get used, because in Silex routes are parsed 'top down'. First match gets used.

sitemap:
    path: /sitemap
    defaults: 
        _controller: 'Bolt\Extension\Bolt\Sitemap\Extension::sitemap'

I apologize for the hackyness of this solution.

Bopp
  • 664
  • 3
  • 5
  • Many thanks @Bopp :) I would also like to add a dynamic requirement to the route to limit the slug parameter to a subset of records in the database. I can see from the way the core Bolt routes are written that this is also possible using a callback. However when I try to point the requirement to a method in a Routing class in my extension I don't have access to $app to do a database query. I've tried mounting my Routing class earlier in the code execution but I then don't have access to the Extension.. How do I solve this? – matthew Apr 11 '15 at 14:16