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

查看:70
本文介绍了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:

假定来自不兼容上下文的$this,则不应静态调用非静态方法Illuminate\Http\Request::all()

Non-static method Illuminate\Http\Request::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 Illuminate\Http\Request;

收件人

use Request;

它应该开始工作.

在config/app.php文件中,您可以找到类别名的列表.在那里,您将看到基类Request已被别名为Illuminate\Support\Facades\Request类.因此,要在命名空间文件中使用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 Illuminate\Support\Facades\Request 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 Illuminate\Http\Request;语句包含在新的Controller模板中,以帮助将开发人员推向使用依赖项注入而不是依赖Facade的方向.

While the above is still technically correct and will work, the use Illuminate\Http\Request; 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中可用)时,应该注入的是Illuminate\Http\Request对象,而不是Request外观.

When injecting the Request object into the constructor (or methods, as available in Laravel 5), it is the Illuminate\Http\Request 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 App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class UserController extends Controller {

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

通过构造函数的示例

<?php namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

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天全站免登陆