You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -564,10 +564,10 @@ Remember, some actions may correspond to policy methods like `create` that do no
564
564
}
565
565
}
566
566
567
-
<aname="via-controller-helpers"></a>
568
-
### Via Controller Helpers
567
+
<aname="via-the-gate-facade"></a>
568
+
### Via the `Gate` Facade
569
569
570
-
In addition to helpful methods provided to the `App\Models\User` model, Laravel provides a helpful `authorize` method to any of your controllers which extend the `App\Http\Controllers\Controller` base class.
570
+
In addition to helpful methods provided to the `App\Models\User` model, you can always authorize actions via the `Gate` facade's `authorize` method.
571
571
572
572
Like the `can` method, this method accepts the name of the action you wish to authorize and the relevant model. If the action is not authorized, the `authorize` method will throw an `Illuminate\Auth\Access\AuthorizationException` exception which the Laravel exception handler will automatically convert to an HTTP response with a 403 status code:
573
573
@@ -579,6 +579,7 @@ Like the `can` method, this method accepts the name of the action you wish to au
579
579
use App\Models\Post;
580
580
use Illuminate\Http\RedirectResponse;
581
581
use Illuminate\Http\Request;
582
+
use Illuminate\Support\Facades\Gate;
582
583
583
584
class PostController extends Controller
584
585
{
@@ -589,7 +590,7 @@ Like the `can` method, this method accepts the name of the action you wish to au
589
590
*/
590
591
public function update(Request $request, Post $post): RedirectResponse
591
592
{
592
-
$this->authorize('update', $post);
593
+
Gate::authorize('update', $post);
593
594
594
595
// The current user can update the blog post...
595
596
@@ -605,6 +606,7 @@ As previously discussed, some policy methods like `create` do not require a mode
605
606
use App\Models\Post;
606
607
use Illuminate\Http\RedirectResponse;
607
608
use Illuminate\Http\Request;
609
+
use Illuminate\Support\Facades\Gate;
608
610
609
611
/**
610
612
* Create a new blog post.
@@ -613,61 +615,17 @@ As previously discussed, some policy methods like `create` do not require a mode
613
615
*/
614
616
public function create(Request $request): RedirectResponse
615
617
{
616
-
$this->authorize('create', Post::class);
618
+
Gate::authorize('create', Post::class);
617
619
618
620
// The current user can create blog posts...
619
621
620
622
return redirect('/posts');
621
623
}
622
624
623
-
<aname="authorizing-resource-controllers"></a>
624
-
#### Authorizing Resource Controllers
625
-
626
-
If you are utilizing [resource controllers](/docs/{{version}}/controllers#resource-controllers), you may make use of the `authorizeResource` method in your controller's constructor. This method will attach the appropriate `can` middleware definitions to the resource controller's methods.
627
-
628
-
The `authorizeResource` method accepts the model's class name as its first argument, and the name of the route / request parameter that will contain the model's ID as its second argument. You should ensure your [resource controller](/docs/{{version}}/controllers#resource-controllers) is created using the `--model` flag so that it has the required method signatures and type hints:
629
-
630
-
<?php
631
-
632
-
namespace App\Http\Controllers;
633
-
634
-
use App\Http\Controllers\Controller;
635
-
use App\Models\Post;
636
-
637
-
class PostController extends Controller
638
-
{
639
-
/**
640
-
* Create the controller instance.
641
-
*/
642
-
public function __construct()
643
-
{
644
-
$this->authorizeResource(Post::class, 'post');
645
-
}
646
-
}
647
-
648
-
The following controller methods will be mapped to their corresponding policy method. When requests are routed to the given controller method, the corresponding policy method will automatically be invoked before the controller method is executed:
649
-
650
-
<divclass="overflow-auto">
651
-
652
-
| Controller Method | Policy Method |
653
-
| --- | --- |
654
-
| index | viewAny |
655
-
| show | view |
656
-
| create | create |
657
-
| store | create |
658
-
| edit | update |
659
-
| update | update |
660
-
| destroy | delete |
661
-
662
-
</div>
663
-
664
-
> [!NOTE]
665
-
> You may use the `make:policy` command with the `--model` option to quickly generate a policy class for a given model: `php artisan make:policy PostPolicy --model=Post`.
666
-
667
625
<aname="via-middleware"></a>
668
626
### Via Middleware
669
627
670
-
Laravel includes a middleware that can authorize actions before the incoming request even reaches your routes or controllers. By default, the `Illuminate\Auth\Middleware\Authorize` middleware is assigned the `can` key in your `App\Http\Kernel` class. Let's explore an example of using the `can` middleware to authorize that a user can update a post:
628
+
Laravel includes a middleware that can authorize actions before the incoming request even reaches your routes or controllers. By default, the `Illuminate\Auth\Middleware\Authorize` middleware is assigned the `can` key in your application's `App\Http\Kernel` class. Let's explore an example of using the `can` middleware to authorize that a user can update a post:
0 commit comments