Skip to content

Commit 745bab3

Browse files
committed
document gate facade
1 parent d3cfb8c commit 745bab3

File tree

1 file changed

+9
-51
lines changed

1 file changed

+9
-51
lines changed

Diff for: authorization.md

+9-51
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
- [Policy Filters](#policy-filters)
1919
- [Authorizing Actions Using Policies](#authorizing-actions-using-policies)
2020
- [Via the User Model](#via-the-user-model)
21-
- [Via Controller Helpers](#via-controller-helpers)
21+
- [Via the Gate Facade](#via-the-gate-facade)
2222
- [Via Middleware](#via-middleware)
2323
- [Via Blade Templates](#via-blade-templates)
2424
- [Supplying Additional Context](#supplying-additional-context)
@@ -564,10 +564,10 @@ Remember, some actions may correspond to policy methods like `create` that do no
564564
}
565565
}
566566

567-
<a name="via-controller-helpers"></a>
568-
### Via Controller Helpers
567+
<a name="via-the-gate-facade"></a>
568+
### Via the `Gate` Facade
569569

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.
571571

572572
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:
573573

@@ -579,6 +579,7 @@ Like the `can` method, this method accepts the name of the action you wish to au
579579
use App\Models\Post;
580580
use Illuminate\Http\RedirectResponse;
581581
use Illuminate\Http\Request;
582+
use Illuminate\Support\Facades\Gate;
582583

583584
class PostController extends Controller
584585
{
@@ -589,7 +590,7 @@ Like the `can` method, this method accepts the name of the action you wish to au
589590
*/
590591
public function update(Request $request, Post $post): RedirectResponse
591592
{
592-
$this->authorize('update', $post);
593+
Gate::authorize('update', $post);
593594

594595
// The current user can update the blog post...
595596

@@ -605,6 +606,7 @@ As previously discussed, some policy methods like `create` do not require a mode
605606
use App\Models\Post;
606607
use Illuminate\Http\RedirectResponse;
607608
use Illuminate\Http\Request;
609+
use Illuminate\Support\Facades\Gate;
608610

609611
/**
610612
* Create a new blog post.
@@ -613,61 +615,17 @@ As previously discussed, some policy methods like `create` do not require a mode
613615
*/
614616
public function create(Request $request): RedirectResponse
615617
{
616-
$this->authorize('create', Post::class);
618+
Gate::authorize('create', Post::class);
617619

618620
// The current user can create blog posts...
619621

620622
return redirect('/posts');
621623
}
622624

623-
<a name="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-
<div class="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-
667625
<a name="via-middleware"></a>
668626
### Via Middleware
669627

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:
671629

672630
use App\Models\Post;
673631

0 commit comments

Comments
 (0)