我在laravel 5.4中遇到了授权政策的问题。我的代码如下所示:
ManagerPolicy.php
<?php
namespace App\Policies;
use App\Models\User;
use App\Models\Manager;
use Illuminate\Auth\Access\HandlesAuthorization;
use Illuminate\Support\Facades\Auth;
class ManagerPolicy
{
use HandlesAuthorization;
public function view(Manager $manager)
{
echo 1;
die;
return $manager->hasAccess('manages.view');
}
}
?>
AuthServiceProvider.php
<?php
namespace App\Providers;
use App\Models\Manager;
use App\Policies\ManagerPolicy;
use Illuminate\Support\Facades\Gate;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
class AuthServiceProvider extends ServiceProvider
{
/**
* The policy mappings for the application.
*
* @var array
*/
protected $policies = [
'App\Model' => 'App\Policies\ModelPolicy',
Manager::class => ManagerPolicy::class
];
/**
* Register any authentication / authorization services.
*
* @return void
*/
public function boot(Gate $gate)
{
$this->registerPolicies($gate);
//
}
}
?>
在我的刀片模板视图中
@can('view', App\Models\Manager::class)
<li>
<a href="{!! route('manages.index') !!}">
<i class="fa fa-user-o"></i> <span> Manager </span>
</a>
</li>
@endcan
当我在“ManagerPolicy @ view”中运行die()
命令时,它对我不起作用。