This forum is in READ-ONLY mode.
You can look around, but if you want to ask a new question, please use Stack Overflow.

[Router] How to match 2 routes having the same pattern

New topics about Symfony 2 should go here

[Router] How to match 2 routes having the same pattern

by Damien Gavard » Mon May 18, 2015 12:58 pm

Hello

I have 2 legacy routes that share the same pattern (/{country} and /{user). I'm looking for a simple way match the right route. It means the UrlMatcher should be able to do more test than using regular expression. For example : checking if a Country or User exist with the given name before calling the router. Do you know any existing component that can do that ?

Code: Select all
$loader = require_once __DIR__.'/autoload.php';

use Symfony\Component\Routing\Matcher\UrlMatcher;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;

$routes = new RouteCollection();

$routes->add('country', new Route('/{country}', ['controller' => 'CountryController']));
$routes->add('user', new Route('/{user}', ['controller' => 'UserController']));

$context = new RequestContext('');

$matcher = new UrlMatcher($routes, $context);

var_export($matcher->match('/foo'));
#array (
#  'controller' => 'CountryController',
#  'country' => 'foo',
#  '_route' => 'country',
#)
Damien Gavard
Junior Member
 
Posts: 1
Joined: Mon May 18, 2015 12:45 pm