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 ...
随机推荐
- docker 实践四:仓库管理
本篇我们来了解 docker 仓库的内容. 注:环境为 CentOS7,docker 19.03 仓库(Responsitory)是集中存放镜像的地方,又分公共仓库和私有仓库. 注:有时候容易把仓库与 ...
- go 构造切片slice
定义切片 make([]int, 5) 长度和容量均为5 make([]int, 0, 5) 长度为0 容量为0 切片 slice2[3:5] 对slice2进行切片返回 第3 4 两个元素 不包含 ...
- Centos7.3 为php7 安装swoole 扩展
今天心血来潮想在服务器上安装一下swoole扩展 下面列一下教程: xshell进入你的服务器 然后目录自选吧 反正我放在根目录了 下面是扩展链接: wget https://github.co ...
- IDEA整合Jenkins界面化管理项目构建
版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/WALK_MAN_wubiao/articl ...
- hdu 4501三重包问题
好好理解一下背包问题 从01包入手 内层的循环 是为了以后求解记录数据 因为只有一个取舍问题 所以只需要一层循环就可以 这里有三个背包 钱 积分 以及免费物品 那么 就需要三重循环 #include& ...
- Linux每隔1秒kill掉cpu大于50%的进程
1.新建/test/killcpu.sh shell脚本 并授予权限0755#!/bin/bashps axf -o "pid %cpu" | awk '{if($2>=50 ...
- findstr 命令使用
findstr 命令使用 find /? 在文件中搜索字符串. FIND [/V] [/C] [/N] [/I] [/OFF[LINE]] "string" [[drive:][p ...
- JavaScript指定日期格式化
formatDataToString:function (dates, formats) { var o = { "M+": dates.getMonth() + 1, //月份 ...
- Uniswap详解之一(概览)
一.Uniswap简介 Uniswap是以太坊上的DEX实现,基于"恒定乘积自动做市"模型,与传统的中心化和DEX具有很大的差别. 主要特点: 无订单簿,无做市商 兑换币具有很低的 ...
- elementUI .native修饰符
用第三方组件或者UI框架会自带自身封装的事件,如keyup等,会覆盖原生的组件而无法起效果 .native 修饰符就是用来注册元素的原生事件而不是组件自定义事件的 如elementUI的:<el ...