MikO
MikO

Reputation: 18751

Symfony: get parameter names from URL string

Given a URL string like:

'my-route/{someId}/{someOption}'

I'd like to get an array like:

['someId', 'someOption']

Obviously I can parse it manually with some regexp, but I was wondering if it'd be possible to do it using some Symfony (2.6+) component or something...

So from wherever in my code I can do something like:

$urlParamNames = $someComponent->getParamNamesFromUrl('my-route/{someId}/{someOption}');

NOTE: I know this is not "standard" Symfony, and I know you'll normally get the parameters from the route injected into the action as arguments and so on... Explaining why I want this would be a very long story though, so just assume I need exactly that functionality, no other advices/suggestions...

Upvotes: 1

Views: 1034

Answers (1)

Federkun
Federkun

Reputation: 36989

That's a strange request. But if you really want:

use Symfony\Component\Routing\RouteCompiler;
use Symfony\Component\Routing\Route;

$compiledRoute = RouteCompiler::compile(new Route('my-route/{someId}/{someOption}'));
var_dump($compiledRoute->getVariables());

Output:

array(2) { [0]=> string(6) "someId" [1]=> string(10) "someOption" }

Without using RouteCompiler directly:

use Symfony\Component\Routing\Route;

$route = new Route('my-route/{someId}/{someOption}');
$compiledRoute = $route->compile();
var_dump($compiledRoute->getVariables());

See compiler_class option if you need to do more.

Upvotes: 4

Related Questions