Php 为什么在角色和权限中使用刀片指令?给我解释一下下面的例子
我的任务是在内置应用程序中添加新功能,最后我得到一段代码。我不知道它是怎么工作的。 关于AppServiceProvider.phpPhp 为什么在角色和权限中使用刀片指令?给我解释一下下面的例子,php,laravel-5,Php,Laravel 5,我的任务是在内置应用程序中添加新功能,最后我得到一段代码。我不知道它是怎么工作的。 关于AppServiceProvider.php Blade::directive('hasRole', function ($expression) { $exp=explode(",",$expression); return "<?php if(Auth::user()->hasPermission($exp[0],$exp[1])): ?>
Blade::directive('hasRole', function ($expression) {
$exp=explode(",",$expression);
return "<?php if(Auth::user()->hasPermission($exp[0],$exp[1])): ?>";
});
Blade::指令('hasRole',函数($expression){
$exp=explode(“,”,$expression);
返回“”;
});
在刀片上:
@hasRole('travel_calendar','view')
<button class="btn btn-space btn-default btn-big tactive" id="travel_menu_cal" ><i class="icon mdi mdi-calendar"></i> Calendar</button>
@endHasRole
@hasRole('travel\u calendar','view')
历法
@端角色
视图在这段代码中也做了什么。
有人给我解释一下代码。自上而下的内联注释:
// Declare a custom function with name 'hasRole' that can be used in Blade files
Blade::directive('hasRole', function ($expression) {
// If there are comma's in the expression, split those and dump the resulting strings in an array
$exp=explode(",",$expression);
// Return some php code that evaluates to pseudocode:
// if user has permission to expression
return "<?php if(Auth::user()->hasPermission($exp[0],$exp[1])): ?>";
});
// Use defined function, expression exists of two string: travel_calendar and view
@hasRole('travel_calendar','view')
<button class="btn btn-space btn-default btn-big tactive" id="travel_menu_cal" ><i class="icon mdi mdi-calendar"></i> Calendar</button>
@endHasRole
->hasPermission($exp[0],$exp[1]):这实际上意味着什么。。。我应该在哪里定义视图。。。如果我更改视图的名称。。用户不会看到按钮。。。现在请给我点火。。非常感谢您,$exp[0]和$exp[1]只不过是您使用
explode
创建的数组的前两个索引而已。在您的示例中,$exp[0]
是'travel\u calendar'
,$exp[1]
是'view'
再次感谢您的见解如果有帮助,请投票并勾选答案,以便社区受益!
If user has permission to view travel_calendar
Then show button