before() public static method

Register a callback to run before all Gate checks.
public static before ( callable $callback )
$callback callable
Exemplo n.º 1
0
 /**
  * Register the application services.
  *
  * @return void
  */
 public function register()
 {
     $this->mergeConfigFrom(__DIR__ . '/../config/config.php', $this->packageName);
     \Gate::before(function ($user, $ability, $arguments) {
         $class = config($this->packageName . '.rbacClass');
         $rbac = new $class();
         return $rbac->checkPermission($user, $ability, $arguments);
     });
     $this->app->afterResolving('blade.compiler', function (BladeCompiler $bladeCompiler) {
         $bladeCompiler->directive('role', function ($roles) {
             return "<?php if(auth()->check() && in_array(auth()->user()->role, explode('|', {$roles}))): ?>";
         });
         $bladeCompiler->directive('endrole', function () {
             return '<?php endif; ?>';
         });
     });
 }