Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 拉威尔的能力在这个论点上是多态的吗?_Php_Laravel_Laravel 5.2 - Fatal编程技术网

Php 拉威尔的能力在这个论点上是多态的吗?

Php 拉威尔的能力在这个论点上是多态的吗?,php,laravel,laravel-5.2,Php,Laravel,Laravel 5.2,我的理解是,使用策略定义的能力确实是多态的,这意味着: Gate::allows('update', $post); Gate::allows('update', $comment); 如果两个对象属于不同的类,并且使用不同的策略注册,则将调用不同的函数: protected $policies = [ Post::class => PostPolicy::class, Comment::class => CommentPolicy::class, ]; 虽然在我

我的理解是,使用策略定义的能力确实是多态的,这意味着:

Gate::allows('update', $post);
Gate::allows('update', $comment);
如果两个对象属于不同的类,并且使用不同的策略注册,则将调用不同的函数:

protected $policies = [
    Post::class => PostPolicy::class,
    Comment::class => CommentPolicy::class,
];
虽然在我看来,使用
$gate->define()
定义的能力是非多态的,这意味着使用相同策略名称的两个调用将相互覆盖:

$gate->define('update', function ($user, $post)    { /* THIS IS THROWN AWAY! */ });
$gate->define('update', function ($user, $comment) { /* the last one is kept */ });
这是正确的吗

文档中显示的非多态示例(
updatepost
updatecomment
)的能力名称与策略示例(
update
)中显示的能力名称之间是否存在任何关系


我的意思是,
-post
后缀是由Laravel添加还是推断的?或者这只是一个例子?

策略定义的能力和关卡定义的能力之间有着显著的区别

  • 当您使用门的
    define
    方法时,您的能力名称将以数组键作为能力名称。如果使用相同的名称定义另一个功能(例如,
    update
    ),它将覆盖旧的功能,因为不能有两个同名的数组键。因此在本例中,
    define($ability,$callback)
    中的唯一标识符就是能力

  • 相反,在定义策略类时,能力名称是策略的实际方法名称。因此,您可以有多个方法名称相同的类(例如,
    update
    ),因为在这种情况下,唯一标识符是第一个参数传递的类,所以
    Post::class

在授权检查过程中的某个时刻,Gate类检查并调用基于该评估的策略方法或定义的能力回调

因此,在使用
define
时,您不能有两个同名的能力,因为以下情况是不可能的:

$abilities = [
    'update' => $callback1,
    'update' => $callback2, // this will override the first
]
正如使用
$policies
时一样,您不能将多个策略关联到一个类:

$policies = [
    Post::class => PostPolicy::class,
    Post::class => AnotherPostPolicy::class, // this will override the first one
];
因此,如果您想使用
update
作为多个模型的能力名称,只需使用策略即可

这还应该回答您的最后一个问题:Laravel不会推断或添加任何内容,能力名称要么是传递给
define
的字符串,要么是您在策略类上定义的方法名称