普通网友 2018-08-11 12:08
浏览 54
已采纳

委托@role和@permission指令不在blade,laravel 5.6中呈现

Let me tell you the points.

  1. I am using Entrust by https://github.com/Zizaco/entrust/
  2. I am using multi-auth i.e I have 2 guards as web for users table and admin guard for admins table.
  3. In admins table there are users with multiple permissions (roles too).
  4. The permission middelware has the code

    if (!Auth::guard('admin')->user()->can(explode('|', $permissions)))

  5. In my Admin model, I have used use EntrustUserTrait;

  6. Middelware permission is working fine on routes.php the only problem I am facing is if any Admin User does not have permission suppose can-create then the html written in the that block should be hidden in blade file.

@permission('can-create') Lorem Ipsum... @endpermission

the above blade directives are not highlighted as @if or @foreach etc.

I think there might be something due multi-auth I am using, since Entrust takes users table by default.

Kindly enlighten me, considering this situation.

  • 写回答

1条回答 默认 最新

  • doushanmo7024 2018-08-13 05:54
    关注

    Yes I got the solution to this issue. Please go through the steps I have used.

    1. Run command php artisan make:provider EntrustCustomServiceProvider
    2. go to app/Providers/EntrustCustomServiceProvider.php
    3. Use Auth; and Write in the boot function

      public function boot() {

      \Blade::directive('permission', function($expression) {
          return "<?php if (Auth::guard('admin')->user()->can({$expression})) : ?>";
      });
      
      \Blade::directive('endpermission', function($expression) {
          return "<?php endif; ?>";
      });
      

      }

    4. Add this entry in providers array in config/app.php : App\Providers\EntrustCustomServiceProvider::class,

    One more thing, many of the phpstorm users may wonder why these directives are not auto-populated in the editor then please note, whenever we add custom blade directives, we need to add these in the blade plugin of the phpstorm editor. Here is the solution:

    1. Go to File->settings->php->Blade
    2. uncheck Default Settings, go to Directives tab.
    3. Add new directive using +
    4. Enter the directive name,here permission without using @, check has parameter
    5. Enter <?php if (Auth::guard('admin')->user()->can( in prefix and )):?> in suffix
    6. same do for @endpermission without adding any parameters.

    I hope this is helpful for someone like me :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?