master

laravel/framework

Last updated at: 29/12/2023 09:23

AuthServiceProvider.php

TLDR

The AuthServiceProvider.php file is a class that extends the ServiceProvider class. It provides methods to register and manage policies for the application.

Methods

register

This method is responsible for registering the application's policies. It invokes the registerPolicies method during the booting process.

registerPolicies

This method registers the application's policies by looping through the $policies property and calling the Gate::policy method for each policy.

policies

This method returns the policies defined on the provider.

Classes (No classes in this file)

<?php

namespace Illuminate\Foundation\Support\Providers;

use Illuminate\Support\Facades\Gate;
use Illuminate\Support\ServiceProvider;

class AuthServiceProvider extends ServiceProvider
{
    /**
     * The policy mappings for the application.
     *
     * @var array<class-string, class-string>
     */
    protected $policies = [];

    /**
     * Register the application's policies.
     *
     * @return void
     */
    public function register()
    {
        $this->booting(function () {
            $this->registerPolicies();
        });
    }

    /**
     * Register the application's policies.
     *
     * @return void
     */
    public function registerPolicies()
    {
        foreach ($this->policies() as $model => $policy) {
            Gate::policy($model, $policy);
        }
    }

    /**
     * Get the policies defined on the provider.
     *
     * @return array<class-string, class-string>
     */
    public function policies()
    {
        return $this->policies;
    }
}