简体   繁体   English

Laravel 5.7 Gate::allows 非静态方法 Illuminate\\Auth\\Access\\Gate::allows() 不应静态调用

[英]Laravel 5.7 Gate::allows Non-static method Illuminate\Auth\Access\Gate::allows() should not be called statically

I got this error in Laravel 5.7 when using Gate::allows in my formRequest class.在我的 formRequest 类中使用 Gate::allows 时,我在 Laravel 5.7 中遇到了这个错误。

I was using this: use Illuminate\\Auth\\Access\\Gate;我正在使用这个: use Illuminate\\Auth\\Access\\Gate; and it didn't work,它没有用,

so I replaced it with this: use Illuminate\\Support\\Facades\\Gate and it worked.所以我用这个替换了它: use Illuminate\\Support\\Facades\\Gate并且它起作用了。

I really need to know why the first didn't work and what is the difference between the two.我真的需要知道为什么第一个不起作用以及两者之间有什么区别。 I have tried looking up stuff but I need a more direct and concise explanation.我曾尝试查找内容,但我需要更直接和简洁的解释。 Any help or pointer will be highly appreciated.任何帮助或指针将不胜感激。

In a Laravel application, a facade is a class that provides access to an object from the container.在 Laravel 应用程序中,facade 是一个类,它提供对容器中对象的访问。 The machinery that makes this work is in the Facade class.完成这项工作的机器在 Facade 类中。 Laravel's facades, and any custom facades you create, will extend the base Illuminate\\Support\\Facades\\Facade class. Laravel 的门面,以及你创建的任何自定义门面,将扩展基础 Illuminate\\Support\\Facades\\Facade 类。

The Facade base class makes use of the __callStatic() magic-method to defer calls from your facade to an object resolved from the container. Facade 基类使用 __callStatic() 魔法方法将来自 Facade 的调用延迟到从容器解析的对象。 In the example below, a call is made to the Laravel cache system.在下面的示例中,调用了 Laravel 缓存系统。 By glancing at this code, one might assume that the static method get is being called on the Cache class:看一眼这段代码,你可能会假设静态方法 get 是在 Cache 类上调用的:

<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Cache;

class UserController extends Controller
{
    /**
     * Show the profile for the given user.
     *
     * @param  int  $id
     * @return Response
     */
    public function showProfile($id)
    {
        $user = Cache::get('user:'.$id);

        return view('profile', ['user' => $user]);
    }
}

Notice that near the top of the file we are "importing" the Cache facade.请注意,在文件顶部附近,我们“导入”了 Cache 外观。 This facade serves as a proxy to accessing the underlying implementation of the Illuminate\\Contracts\\Cache\\Factory interface.这个门面作为代理访问 Illuminate\\Contracts\\Cache\\Factory 接口的底层实现。 Any calls we make using the facade will be passed to the underlying instance of Laravel's cache service.我们使用 Facade 进行的任何调用都将传递给 Laravel 缓存服务的底层实例。

If we look at that Illuminate\\Support\\Facades\\Cache class, you'll see that there is no static method get:如果我们查看 Illuminate\\Support\\Facades\\Cache 类,您会看到没有静态方法 get:

class Cache extends Facade {
    /**
     * Get the registered name of the component.
     *
     * @return string
     */
    protected static function getFacadeAccessor() { return 'cache'; } }

Instead, the Cache facade extends the base Facade class and defines the method getFacadeAccessor().相反,Cache Facade 扩展了 Facade 基类并定义了 getFacadeAccessor() 方法。 This method's job is to return the name of a service container binding.此方法的工作是返回服务容器绑定的名称。 When a user references any static method on the Cache facade, Laravel resolves the cache binding from the service container and runs the requested method (in this case, get) against that object.当用户引用 Cache 外观上的任何静态方法时,Laravel 会解析来自服务容器的缓存绑定,并针对该对象运行请求的方法(在本例中为 get)。

Docs文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM