教程: https://symfony.com/doc/current/create_framework/templating.html 我遇到了一些合乎逻辑的"跳过"如何通过路由的名称在请求属性的帮助下传递一个匿名体如何从路由中起作用?
'_controller'=> function ($request) {
在路线声明中找到:
$routes->add('hello', new Routing\Route('/hello/{name}', array(
'name' => 'World',
'_controller'=> function ($request) {
return render_template($request);
}
)));
和
$response = call_user_func($request->attributes->get('_controller'),
在前端控制器中找到:
try {
$request->attributes->add($matcher->match($request->getPathInfo()));
$response = call_user_func($request->attributes->get('_controller'),
$request);
} catch (Routing\Exception\ResourceNotFoundException $e) {
$response = new Response('Not Found', 404);
} catch (Exception $e) {
$response = new Response('An error occurred', 500);
}
有人愿意解释它是如何运作的吗?
提前谢谢。答案 0 :(得分:1)
现在好了,它是做必要工作的匹配者。
$matcher->match($request->getPathInfo())
在
try {
$request->attributes->add($matcher->match($request->getPathInfo()));
$response = call_user_func($request->attributes->get('_controller'),
$request);
} catch (Routing\Exception\ResourceNotFoundException $e) {
$response = new Response('Not Found', 404);
} catch (Exception $e) {
$response = new Response('An error occurred', 500);
}