Laravel 5.8: Automatic Policy Resolution
Laravel 5.8: Automatic Policy Resolution
One of the new features in Laravel 5.8 allows you to not register your policies in AuthServiceProvider, they will be “guessed” automatically. Here’s how it works.
Let’s remember how Policies work in general.
Step 1: Create a policy with artisan command, attaching it to a model.
php artisan make:policy PostPolicy --model=Post
Step 2. Fill in the policy with exact methods and rules
use App\User;
use App\Post;
class PostPolicy
{
public function update(User $user, Post $post)
{
return $user->id === $post->user_id;
}
}
Step 3. Register policies (this is needed only before Laravel 5.8)
use App\Post;
use App\Policies\PostPolicy;
class AuthServiceProvider extends ServiceProvider
{
protected $policies = [
Post::class => PostPolicy::class,
];
So, from Laravel 5.8 you don’t need to do step 3, system will recognize the policies automatically.
Here’s how that “guessing” function actually looks in Laravel internals:
protected function guessPolicyName($class)
{
return dirname(str_replace('\\', '/', $class)).'\\Policies\\'.class_basename($class).'Policy';
}
So your Policy should be in app/Policies folder and has the same name as model, with Policy suffix.
Correct location: app/User.php -> app/Policies/UserPolicy.php
Incorrect location: app/Models/User.php -> app/Policies/UserPolicy.php
Also incorrect: app/User.php -> app/Policies/UsersPolicy.php (Users)
And, also incorrect: app/User.php -> app/Policies/User.php (should be UserPolicy.php)
If your policies and models are in “nonconventional locations” (like not in app/ or app/Policies folders, as shown above), then you may use the same $policies array in AuthServiceProvider, or specify your own logic of “guessing”:
Gate::guessPolicyNamesUsing(function ($class) {
// Do stuff
return $policyClass;
});
Here’s a link to the original commit in Laravel.
Laravel 5.8: Automatic Policy Resolution的更多相关文章
- Laravel核心解读--异常处理
异常处理是编程中十分重要但也最容易被人忽视的语言特性,它为开发者提供了处理程序运行时错误的机制,对于程序设计来说正确的异常处理能够防止泄露程序自身细节给用户,给开发者提供完整的错误回溯堆栈,同时也能提 ...
- Oracle12c版本中未归档隐藏参数
In this post, I will give a list of all undocumented parameters in Oracle 12.1.0.1c. Here is a query ...
- Oracle11g版本中未归档隐藏参数
In this post, I will give a list of all undocumented parameters in Oracle 11g. Here is a query to se ...
- Snort Inline IPS Mode
Snort Inline IPS Mode https://forum.netgate.com/topic/143812/snort-package-4-0-inline-ips-mode-intro ...
- Fedora 22中的RPM软件包管理工具
Introduction The RPM Package Manager (RPM) is an open packaging system that runs on Fedora as well a ...
- Fedora 22中的DNF软件包管理工具
Introduction DNF is the The Fedora Project package manager that is able to query for information abo ...
- Yii源码阅读笔记(三十五)
Container,用于动态地创建.注入依赖单元,映射依赖关系等功能,减少了许多代码量,降低代码耦合程度,提高项目的可维护性. namespace yii\di; use ReflectionClas ...
- 【转】 svn 错误 以及 中文翻译
直接Ctrl+F 搜索你要找的错 # # Simplified Chinese translation for subversion package # This file is distribute ...
- flex mxmlc 手动编译项目
首先: 1.下载flex的sdk,如果你电脑有装flash builder,它自带了一份,位于安装目录的sdks目录下. 备注:(sdk依赖java的jre) 2.配置mxmlc的java运行环境jr ...
随机推荐
- 通过jquery触发select自身的change事件
###通过jquery触发select自身的change事件 1.通过js来去触发select的change事件 代码如下:包含了html部分和js部分 //html部分 <select cla ...
- python学习-45 模块
模块 -----模块包括三种: ····python标准库 ····第三方模块 ····应用程序自定义模块 -------应用程序自定义模块 1.建立两个py文件,一个是定义函数用的cal.py de ...
- Spring @Transactional注解在什么情况下会失效,为什么?
出处: https://www.cnblogs.com/hunrry/p/9183209.html https://www.cnblogs.com/protected/p/6652188.htm ...
- Nginx学习笔记(四):基本数据结构
目录 Nginx的一些特点 Nginx自定义整数类型 异常机制错误处理 内存池 字符串 时间与日期 运行日志 Nginx的一些特点 高性能 采用事件驱动模型,可以无阻塞的处理海量并发连接 高稳定性 ...
- C#xml泛型序列化
using System; using System.Collections.Generic; using System.IO; using System.Text; using System.Web ...
- python实现nc
#!/usr/bin/python2 import sys import socket import getopt import thread import subprocess listen =Fa ...
- nexus 匿名用户的问题。
为了做到安全和不浪费我们自己的服务器资源,要绝对拒绝匿名用户进行访问: 1,不允许匿名用户访问 2,禁用匿名的账号 以下是这2点的设置图. ============================== ...
- restTemplate源码解析(四)执行ClientHttpRequest请求对象
所有文章 https://www.cnblogs.com/lay2017/p/11740855.html 正文 上一篇文章中,我们创建了一个ClientHttpRequest的实例.本文将继续阅读Cl ...
- Java 之 IDEA 的 Debug 追踪
使用 IDEA 的断点调试功能,查看程序的运行过程. 1.在有效代码行,点击行号右边的空白区域,设置断点,程序执行到断点将停止,我们可以手动来运行程序 2.点击 Debug 运行模式 3.程序停止在断 ...
- ajax格式,转入后台
setInterval(function(),时间)定时重复发送请求