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

标签 php laravel

在 Laravel 5.7 中,我在 formRequest 类中使用 Gate::allows 时遇到此错误。

我正在使用这个:use Illuminate\Auth\Access\Gate; 但它不起作用,

所以我用这个替换了它:use Illuminate\Support\Facades\Gate 并且它起作用了。

我真的需要知道为什么第一个不起作用以及两者之间有什么区别。我尝试过查找内容,但我需要更直接和简洁的解释。 任何帮助或指示将不胜感激。

最佳答案

In a Laravel application, a facade is a class that provides access to an object from the container. The machinery that makes this work is in the Facade class. Laravel's facades, and any custom facades you create, will extend the base Illuminate\Support\Facades\Facade class.

The Facade base class makes use of the __callStatic() magic-method to defer calls from your facade to an object resolved from the container. In the example below, a call is made to the Laravel cache system. By glancing at this code, one might assume that the static method get is being called on the Cache class:

<?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. This facade serves as a proxy to accessing the underlying implementation of the Illuminate\Contracts\Cache\Factory interface. Any calls we make using the facade will be passed to the underlying instance of Laravel's cache service.

If we look at that Illuminate\Support\Facades\Cache class, you'll see that there is no static method 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(). 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.

Docs

关于php - Laravel 5.7 Gate::allows 非静态方法 Illuminate\Auth\Access\Gate::allows() 不应静态调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59650704/

相关文章:

javascript - 使用 Laravel ORM hasmany 和 Vue 2 在 Laravel 中过滤数据

laravel - 测试通知后数据库通知不显示

php - 依靠最大的 table

php - 拉拉维尔 5.1。无法运行 artisan 命令

php - 您如何在PHP中解析和处理HTML/XML?

php - laravel 在某些情况下自动将数据插入数据库

php - 动态访问嵌套对象

javascript - PHP Post 与 JavaScript Ajax 请求

mysql - 限制 DB::laravel 中多个表的查询

php - 在 Lumen 中执行迁移时未设置表名