简体   繁体   中英

Laravel: Gate::denies not working

So I am trying to use Laravel Authorization . For here I did following:

  • Laravel updated to *.1.19
  • Create The Policies Directory
  • Facade defined in app.php

I created a Policy and put following code in it:

//Allow users of type 'users`    
public function view(User $user)
        {
            dd('he was here'); // Not coming here
            return $user->user_type === 'user';
        }

In Controller I did:

if (Gate::denies('view')) {
            dd('Sorry Bud not allowed');
        }

Thing is, it is always getting into blocks 'Sorry Bud not allowed' .

Am I missing some step or something else?

2 things to check with Gate

First: Make sure your policy is getting registered.

Second: If you have setup custom Guard in config.auth in that case inside your controller add the below line.

config(['auth.defaults.guard','YOUR-CUSTOM-GUARD-NAME']);

Thanks Mage2 Ecommerce

您可能需要注册策略,请参阅文档http://laravel.com/docs/5.1/authorization#policies中的“注册策略”部分。

您还需要将App\\Providers\\AuthServiceProvider::class,添加到config / app.php中的providers数组中。

this is just because you not log in to your project to know the user. so first log in to your project then this code is work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM