【问题标题】:What is the difference between $this->getRequest() and passing Request as a parameter in Symfony 2$this->getRequest() 和在 Symfony 2 中将 Request 作为参数传递有什么区别
【发布时间】:2014-01-25 00:22:12
【问题描述】:

在控制器动作中,我们可以通过两种方式获取请求对象:

$request = $this->getRequest();

或将其作为参数传递给操作

public function testAction(Request $request)

它们之间有什么区别?是否有推荐的方式来获取请求或特定情况时,每个都是首选?

【问题讨论】:

标签: php symfony request


【解决方案1】:

实际上在 Symfony 2.4 之前没有明显的区别。但根据文档 (https://github.com/symfony/symfony/blob/master/UPGRADE-3.0.md),您应该将请求对象作为操作方法参数传递。

基本 Controller 类的 getRequest 方法已被弃用 从 Symfony 2.4 开始,因此必须在 3.0 中删除。唯一的 获取 Request 对象的可靠方法是将其注入到操作中 方法。

【讨论】:

    猜你喜欢
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 2011-09-11
    • 1970-01-01
    • 2010-11-06
    • 2011-04-12
    • 1970-01-01
    相关资源
    最近更新 更多