I'm having the same problem. What version of Laravel are you running and on what box and PHP version? I myself am running on a homestead-7/PHP-7 set-up. I think with Laravel v5.1.24. Even if I "return true;" from the Policy, it does not work.
I am running Apache 2.4 on Windows with PHP Version 5.6.15, mycrypt enabled and MySQL, and it's Laravel 5.1.23. I tried another tutorial where I got the auth()->user->id and that didn't work either so I'm thinking it's to do with the auth/user object. Unfortunately I am brand new to laravel so don't know how to prove/disprove this... Thinking of going back to Symfony2 but heard Laravel was better + easier to use... Not for me so far I'm afraid! Would love to be proven wrong though!
I can get this to work:
AuthServiceProvider.php:
public function boot(GateContract $gate)
{
$this->registerPolicies($gate);
$gate->define('delete-project', function($user, $project) {
return $user->id == $project->user_id;
});
}
ProjectController.php:
public function destroy(Request $request, Project $project)
{
//$this->authorize('destroy', $project);
//$project->delete();
if(Gate::allows('delete-project', $project)) {
$project->delete();
} else {
abort(403);
}
return redirect('/projects');
}
But the $this->authorize() and use of policies doesn't work...
Okay I got the policies working. I had to add at the top of AuthServiceProvider.php these lines:
use App\Project;
use App\Policies\ProjectPolicy;
...
public function boot(GateContract $gate)
{
//parent::registerPolicies($gate);
$this->registerPolicies($gate);
}
Ah, good find! Works here also now. I see they do do it here: http://laravel.com/docs/5.1/authorization#creating-policies Maybe someone should edit the tutorials to explicitly point it out..
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.