Laravel 5.7 Gate::allows 非静态方法 Illuminate

在我的 formRequest 类中使用 Gate::allows 时,我在 Laravel 5.7 中遇到了这个错误。

我正在使用这个:use Illuminate\Auth\Access\Gate;它没有用,

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

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


慕桂英3389331
浏览 123回答 1
1回答

吃鸡游戏

在 Laravel 应用程序中,外观是一个提供从容器访问对象的类。完成这项工作的机器在 Facade 类中。Laravel 的外观,以及您创建的任何自定义外观,都将扩展基础 Illuminate\Support\Facades\Facade 类。Facade 基类使用 __callStatic() 魔术方法将来自外观的调用推迟到从容器解析的对象。在下面的示例中,调用了 Laravel 缓存系统。看一眼这段代码,人们可能会认为静态方法 get 正在 Cache 类上被调用:<?phpnamespace App\Http\Controllers;use App\Http\Controllers\Controller;use Illuminate\Support\Facades\Cache;class UserController extends Controller{&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Show the profile for the given user.&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @param&nbsp; int&nbsp; $id&nbsp; &nbsp; &nbsp;* @return Response&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public function showProfile($id)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $user = Cache::get('user:'.$id);&nbsp; &nbsp; &nbsp; &nbsp; return view('profile', ['user' => $user]);&nbsp; &nbsp; }}请注意,在文件顶部附近,我们正在“导入” Cache 外观。这个门面用作访问 Illuminate\Contracts\Cache\Factory 接口的底层实现的代理。我们使用外观进行的任何调用都将传递给 Laravel 缓存服务的底层实例。如果我们查看 Illuminate\Support\Facades\Cache 类,您会发现没有静态方法 get:class Cache extends Facade {&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Get the registered name of the component.&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @return string&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; protected static function getFacadeAccessor() { return 'cache'; } }相反,Cache 外观扩展了基本外观类并定义了方法 getFacadeAccessor()。此方法的工作是返回服务容器绑定的名称。当用户在 Cache 外观上引用任何静态方法时,Laravel 会从服务容器解析缓存绑定并针对该对象运行请求的方法(在本例中为 get)。
打开App,查看更多内容
随时随地看视频慕课网APP