gpt4 book ai didi

php - 使用 Symfony 的路由器和 http-foundation 组件调用类函数的正确方法是什么?

转载 作者:搜寻专家 更新时间:2023-10-31 21:06:15 25 4
gpt4 key购买 nike

我最近将 Symfony 的 Routing 和 HttpFoundation 组件插入到我的遗留应用程序中,并且我正在慢慢开始转换我的所有代码。

到目前为止,一切都运行得非常好,但我认为我正在以我构建 Controller 的方式和路由器调用它们的方式进入一个兔子洞。这是我的前端 Controller 中的一些代码:

// look up the controller and action defined in routes.yml
$parameter = $router->match($request->getPathInfo());

// call the action and get the output
$output = call_user_func('MyCompany\\Controller\\' . $parameter['_controller']);

// send the output in the response and so forth
// ....

因此,call_user_func 行实际上会调用类似 MyCompany\Controller\GeneralController::indexAction 的内容,它最终会返回 html 输出,并在响应中发送。

由于我设置所有内容的方式,我必须使用像 return self::display('filename.tpl'); 这样的语句。这对我来说似乎是错误的。有没有更好的方法来调用这些 Controller 操作?

最佳答案

您可能想要使用 ControllerResolver .或者您可能想更直接地使用 HttpKernel,它会为您解决问题。

如果您使用 ControllerResolver,它将创建一个“可调用对象”,并获取您可以执行的参数 $response = call_user_func_array($controller, $arguments);

使用 HttpKernel 或 AppKernel 还有很多其他好处,它们提供了更多的脚手架。

关于php - 使用 Symfony 的路由器和 http-foundation 组件调用类函数的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31976989/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com