Automatic Policy Resolution is Coming to Laravel 5.8

Automatic Policy Resolution is Coming to Laravel 5.8

Starting in Laravel 5.8, as long as policies and models are in the conventional locations, you will not need to register them in the AuthServiceProvider class:

If you have your models or policies in nonconventional locations, you can customize the policy guessing logic used to “look up” policies via the Gate:

Gate::guessPolicyNamesUsing(function ($class) {
    // Do stuff
    return $policyClass;
});

Laravel 5.7 determines policies from a configuration array found in the AuthServiceProvider with the model as the key mapping to the associated policy:

protected $policies = [
    'App\Model' => 'App\Policies\ModelPolicy'
];

You can continue to use the $policies array to define your policies manually in Laravel 5.8, however, if you create new policies using the typical class path conventions for models and policies you don’t have to worry about adding additional policies to the configuration.

If you’re interested in the code that made this feature possible, here are a few commits you might want to check out:


Filed in: Laravel 5.8


Newsletter

Join the weekly newsletter and never miss out on new tips, tutorials, and more.

Laravel News Partners

Laravel Jobs

Senior Developer for a Algo Strategy Marketplace Startup
Mumbai
Tradetron
Software Engineer
Remote
Quartzy
Laravel Developer
South Florida (Remote considered)
Marine Connection
Senior Software Engineer - LAMP/PHP/MySQL/Laravel
Los Angeles / Remote
Ranker
Senior PHP/Laravel Developer: Your Dream Work Environment
Remote
iPhone Photography School
In-house Laravel Developer.
Gold Coast / Australia
MXstore
Laravel Developer
Oak Brook, IL
Tidal Commerce