Laravel:Gate ::拒绝不工作

问题描述 投票:3回答:4

所以我想尝试使用Laravel Authorization。对于here我做了以下:

  • Laravel更新为* .1.19
  • 创建策略目录
  • 在app.php中定义的Facade

我创建了一个策略并在其中放入以下代码:

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

在控制器中我做了:

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

事情是,它总是进入块'Sorry Bud not allowed'

我错过了一些步骤还是别的什么?

php authorization laravel-5.1
4个回答
1
投票

用Gate检查2件事

第一:确保您的政策已注册。

第二:如果你在config.auth中设置了自定义Guard,那么在你的控制器中添加以下行。

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

谢谢Mage2 Ecommerce


0
投票

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


0
投票

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


0
投票

这只是因为您没有登录到您的项目以了解用户。所以首先登录你的项目然后这个代码是有效的。

© www.soinside.com 2019 - 2024. All rights reserved.