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. MySQL 触发器的使用

    MySQL 基础篇 三范式 MySQL 军规 MySQL 配置 MySQL 用户管理和权限设置 MySQL 常用函数介绍 MySQL 字段类型介绍 MySQL 多列排序 MySQL 行转列 列转行 M ...

  2. 怎样理解window.name

    window.name表示当前窗口的名字, 而非网页的名字, 网页的名字需要使用: document.title; window.name一般是空的字符串, 他的作用其实是配合配合超链接和表单的tar ...

  3. hdu 4501三重包问题

    好好理解一下背包问题 从01包入手 内层的循环 是为了以后求解记录数据 因为只有一个取舍问题 所以只需要一层循环就可以 这里有三个背包 钱 积分 以及免费物品 那么 就需要三重循环 #include& ...

  4. (四)resultMap、sql片段与动态SQL

    一.resultMap 1.1 为什么要用resultMap resultType:指定输出结果的类型(pojo.简单类型.hashmap),将SQL查询结果映射为Java对象. 使用resultTy ...

  5. (二)发布第一个WebService服务与DSWL文档解析

    1. 编写接口 package service; import javax.jws.WebService; /** * 第一个webservice服务, * @WebService注解表示这是一个we ...

  6. 在docker下SQL Server attach mdf和ldf文件

    (DB:MyPost) USE masterGO-- Create database via attachCREATE DATABASE [MyPost]    ON ( FILENAME = N'C ...

  7. 七、Flex 布局

    布局的传统解决方案,基于盒状模型,依赖 display 属性 + position属性 + float属性.它对于那些特殊布局非常不方便,比如,垂直居中就不容易实现. 2009年,W3C 提出了一种新 ...

  8. ASP.NET WEB应用程序(.network4.5)MVC Razor视图引擎2 HtmlHelper-超链接方法

    一.@Html.ActionLink()概述 在MVC的Rasor视图引擎中,微软采用一种全新的方式来表示从前的超链接方式,它代替了从前的繁杂的超链接标签,让代码看起来更加简洁.通过浏览器依然会解析成 ...

  9. Spring中Bean的管理问题

    首先,配置文件中定义的bean并不是都在启动时实例化. <bean id="accountService" class="com.foo.DefaultAccoun ...

  10. [Vue warn]: Duplicate keys detected: '0'. This may cause an update error.

    1.[Vue warn]: Duplicate keys detected: '0'. This may cause an update error. 第一眼看到这个错误一脸懵逼,项目使用很久了,代码 ...