Laravel Request::all() 不应静态调用 [英] Laravel Request::all() Should Not Be Called Statically

查看:29
本文介绍了Laravel Request::all() 不应静态调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Laravel 中,我试图在控制器中的 store() 方法上调用 $input = Request::all();,但我收到以下错误:

In Laravel, I'm trying to call $input = Request::all(); on a store() method in my controller, but I'm getting the following error:

非静态方法 IlluminateHttpRequest::all() 不应静态调用,假设 $this 来自不兼容的上下文

Non-static method IlluminateHttpRequest::all() should not be called statically, assuming $this from incompatible context

是否有任何帮助找出解决此问题的最佳方法?(我正在关注 Laracast)

Any help figuring out the best way to correct this? (I'm following a Laracast)

推荐答案

错误消息是由于调用没有通过 Request 门面.

The error message is due to the call not going through the Request facade.

改变

use IlluminateHttpRequest;

use Request;

它应该开始工作了.

在 config/app.php 文件中,您可以找到类别名的列表.在那里,您将看到基类 Request 已被别名为 IlluminateSupportFacadesRequest 类.因此,要在命名空间文件中使用 Request 门面,您需要指定使用基类:use Request;.

In the config/app.php file, you can find a list of the class aliases. There, you will see that the base class Request has been aliased to the IlluminateSupportFacadesRequest class. Because of this, to use the Request facade in a namespaced file, you need to specify to use the base class: use Request;.

由于这个问题似乎有点流量,所以我想在 Laravel 5 正式发布后稍微更新一下答案.

Since this question seems to get some traffic, I wanted to update the answer a little bit since Laravel 5 was officially released.

虽然以上在技术上仍然是正确的并且可以工作,但 use IlluminateHttpRequest; 语句包含在新的控制器模板中,以帮助推动开发人员朝着使用依赖注入而不是依赖的方向发展在立面上.

While the above is still technically correct and will work, the use IlluminateHttpRequest; statement is included in the new Controller template to help push developers in the direction of using dependency injection versus relying on the Facade.

当将 Request 对象注入构造函数(或 Laravel 5 中可用的方法)时,应该注入的是 IlluminateHttpRequest 对象,而不是 Request 门面.

When injecting the Request object into the constructor (or methods, as available in Laravel 5), it is the IlluminateHttpRequest object that should be injected, and not the Request facade.

因此,与其更改 Controller 模板以使用 Request Facade,不如建议使用给定的 Controller 模板并转向使用依赖项注入(通过构造函数或方法).

So, instead of changing the Controller template to work with the Request facade, it is better recommended to work with the given Controller template and move towards using dependency injection (via constructor or methods).

方法示例

<?php namespace AppHttpControllers;

use AppHttpControllersController;
use IlluminateHttpRequest;

class UserController extends Controller {

    /**
     * Store a newly created resource in storage.
     *
     * @param  IlluminateHttpRequest  $request
     * @return Response
     */
    public function store(Request $request) {
        $name = $request->input('name');
    }
}

通过构造函数的例子

<?php namespace AppHttpControllers;

use AppHttpControllersController;
use IlluminateHttpRequest;

class UserController extends Controller {

    protected $request;

    public function __construct(Request $request) {
        $this->request = $request;
    }

    /**
     * Store a newly created resource in storage.
     *
     * @return Response
     */
    public function store() {
        $name = $this->request->input('name');
    }
}

这篇关于Laravel Request::all() 不应静态调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆