Silex动态路由动态调用Class \ Controller,就像Yii一样

I'm not able to find a way to use dynamic routing with silex in the way that Yii does.

For example Yii in the config.php has the following routing definitions:

'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',

So it dynamically call the controller based on the url path. So how it's possible to do it in Silex? I'm trying something like that but it doesn't work:

$app->match('/{controller}/{action}', function($controller,$action) {
  $controller = ucwords($controller);
  $name = "Def\Controller\{$controller}Controller::{$action}Action";
  return new $name;
})->method('GET');

It looks like your $name variable has a problem because you are using single backslashes, which is the escape character. Also, avoid using {%variable} syntax. It is better to concatenate the string's elements with the (.) dot operator, as this is much less error-prone and the code is easier to read this way.

Try:

$name = "Def\\Controller\\".$controller."Controller::".$action."Action";