我想在我的小站点中使用独立的 Symfony2 路由组件。我根据文档和一些示例创建了这个:
$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());
我不明白应该如何调用我已传递给路由构造函数的 Controller 测试。因此,我想要得到类似 Silex Route 匹配的东西:
$app->get('/hello/{name}', function($name) use($app) {
return 'Hello '.$app->escape($name);
});
抱歉我的英语...
最佳答案
$matcher->match()
返回[ 1 ] 匹配路由的属性[ 2 ] (包括一个特殊的 _route
属性,其中包含路由名称 [ 3 ])。
controller
默认值也包含在属性中,因此您可以轻松访问它,然后使用类似 call_user_func
的内容。调用 Controller :
// ...
$attributes = $match->match($request->getPathInfo());
$controllerResult = call_user_func($attributes['controller']);
关于php - 如何使用 Symfony2 路由组件将路由与 Controller 绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20815628/