Help on authorizeResource() alternative in Laravel 11
In laravel 10 I can use authorizeResource() if I utilize resource controller via Controller helper but in laravel 11 there is no 'via controller helper' section but replaced by 'via Gate facade'. Does using
Gate::authorize('create', Post::class);
will achieve the same result as
$this->authorizeResource(Post::class, 'create'); ?
use App\Models\Post;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
class PostController extends Controller
{
use AuthorizesRequests;
public function __construct()
{
$this->authorizeResource(Post::class, 'post');
}
}
You can also add it to app/Http/Controllers/Controller.php
In previous Laravel versions, AuthorizesRequests was used automatically in the base Controller class. And the documentation suggested to use $this->authorize() to check the authorizations defined via policies.
But in the last version (11), it's recommended to use Gate::authorize() instead.
Is it exactly the same code executed under the hood ? Or is it different ?