1

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...

2
  • first, symfony injects them as parameters into your action method. just define $someId and $someOption as parameters. second, you should be able to extract the parameters with the routing service Commented Oct 27, 2015 at 12:15
  • @Joshua I know :) See the note I added... Commented Oct 27, 2015 at 12:18

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.