Laravel 5.8: Automatic Policy Resolution

March 26, 2019

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的更多相关文章

  1. Laravel核心解读--异常处理

    异常处理是编程中十分重要但也最容易被人忽视的语言特性,它为开发者提供了处理程序运行时错误的机制,对于程序设计来说正确的异常处理能够防止泄露程序自身细节给用户,给开发者提供完整的错误回溯堆栈,同时也能提 ...

  2. Oracle12c版本中未归档隐藏参数

    In this post, I will give a list of all undocumented parameters in Oracle 12.1.0.1c. Here is a query ...

  3. Oracle11g版本中未归档隐藏参数

    In this post, I will give a list of all undocumented parameters in Oracle 11g. Here is a query to se ...

  4. Snort Inline IPS Mode

    Snort Inline IPS Mode https://forum.netgate.com/topic/143812/snort-package-4-0-inline-ips-mode-intro ...

  5. Fedora 22中的RPM软件包管理工具

    Introduction The RPM Package Manager (RPM) is an open packaging system that runs on Fedora as well a ...

  6. Fedora 22中的DNF软件包管理工具

    Introduction DNF is the The Fedora Project package manager that is able to query for information abo ...

  7. Yii源码阅读笔记(三十五)

    Container,用于动态地创建.注入依赖单元,映射依赖关系等功能,减少了许多代码量,降低代码耦合程度,提高项目的可维护性. namespace yii\di; use ReflectionClas ...

  8. 【转】 svn 错误 以及 中文翻译

    直接Ctrl+F 搜索你要找的错 # # Simplified Chinese translation for subversion package # This file is distribute ...

  9. flex mxmlc 手动编译项目

    首先: 1.下载flex的sdk,如果你电脑有装flash builder,它自带了一份,位于安装目录的sdks目录下. 备注:(sdk依赖java的jre) 2.配置mxmlc的java运行环境jr ...

随机推荐

  1. Centos7.3安装sftp服务和ssh

    Centos安装SFTP 安装SFTP服务         1. 查看openssh版本             ssh -V             openssh版本必须大于4.8p1       ...

  2. 十四、i2c子系统

    由于之后的触摸屏驱动分析中使用到了GPIO子系统和i2c子系统,因此在分析触摸屏驱动之前我准备把这两个子系统进行简单分析. 在读者学习本章以及后续i2c相关章节之前,最好了解i2c通信方式,可以参考: ...

  3. 少儿编程 | 02.Scratch编程环境

    上次课程介绍了Scratch的基本概念和一些特点,最后还给出了一些有趣的例子.本次课程介绍Scratch的两种编程环境以及在Scratch官网注册个人账号的步骤. Scratch 3.0的两种编程环境 ...

  4. hdu 6208 上一个kmp模板

    #include <cstdio> #include <cstring> #include <iostream> #include <queue> #i ...

  5. JAVA的转义字符

    一.常见的转义字符 转移字符对应的英文是escape character  , 转义字符串(Escape Sequence) 字母前面加上捺斜线"\"来表示常见的那些不能显示的AS ...

  6. Dapper 入门

    中文文档连接:https://www.w3cschool.cn/dapperorm/dapperorm-toj931f2.html 官网文档连接:https://dapper-tutorial.net ...

  7. 【es6】将2个数组合并为一个数组

    //第一种 一个数组中的值为key 一个数组中的值为value let arr1 = ['内存','颜色','尺寸']; let arr2 = [1,2,3]; let temp = arr1.map ...

  8. Spring AOP编程经验总结

    编程范式概览:面向过程,面向对象,函数式编程,事件驱动编程,面向切面等, AOP是什么? Spring AOP是采用面向切面编程的编程范式,而非编程语言,它只能解决特定问题,而非所有问题,它与OOP不 ...

  9. 一款结合nmap及mascan还有shodan的扫描脚本

    github在这里 https://github.com/s0md3v/Silver 很是舒服 Usage Note: Silver scans all TCP ports by default i. ...

  10. linux同步onedrive文件

    定时任务 # 开机自启动 @reboot /root/system/start.sh # 从零点开始每小时执行一次任务 0 0 0/1 * * ? nohup rclone sync onedrive ...