Php 如何使用Symfony2路由组件与控制器绑定路由
我想在我的小站点中使用一个独立的Symfony2路由组件。我根据文档和一些示例创建了这个:Php 如何使用Symfony2路由组件与控制器绑定路由,php,symfony,routing,silex,Php,Symfony,Routing,Silex,我想在我的小站点中使用一个独立的Symfony2路由组件。我根据文档和一些示例创建了这个: $request = Request::createFromGlobals(); $routeTest = new Symfony\Component\Routing\Route('/route-test', array('controller' => 'test')); $routes = new Symfony\Component\Routing\RouteCollection(); $ro
$request = Request::createFromGlobals();
$routeTest = new Symfony\Component\Routing\Route('/route-test', array('controller' => 'test'));
$routes = new Symfony\Component\Routing\RouteCollection();
$routes->add('test', $routeTest);
$context = new Symfony\Component\Routing\RequestContext();
$context->fromRequest($request);
$matcher = new Symfony\Component\Routing\Matcher\UrlMatcher($routes, $context);
$matcher->match($request->getPathInfo());
我不明白我应该如何调用我的控制器测试,我已经传递给路由构造函数了。因此,我希望获得类似Silex Route matching的内容:
$app->get('/hello/{name}', function($name) use($app) {
return 'Hello '.$app->escape($name);
});
对不起,我的英语…$matcher->match()
返回[]匹配路由[]的属性(包括包含路由名称[]的特殊\u路由
属性)
属性中也包含控制器
默认值,因此您可以方便地访问它,然后使用类似的方法调用控制器:
// ...
$attributes = $match->match($request->getPathInfo());
$controllerResult = call_user_func($attributes['controller']);