master

laravel/framework

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

RouteServiceProvider.php

TLDR

The RouteServiceProvider.php file in the Illuminate\Foundation\Support\Providers namespace is a service provider class that is responsible for registering application routes. It extends the ServiceProvider class and includes methods for registering and loading routes.

Methods

register

This method is responsible for registering any application services. It sets the root controller namespace, loads routes (either from cache or using a callback), and refreshes route name and action lookups.

boot

This method is responsible for bootstrapping any application services. Currently, it does nothing.

routes

This method is a protected method that allows registering a callback to load the application's routes.

loadRoutesUsing

This is a static method that allows setting a global callback to load the application's routes.

setRootControllerNamespace

This is a protected method that sets the root controller namespace for the application.

routesAreCached

This is a protected method that determines if the application routes are cached.

loadCachedRoutes

This is a protected method that loads the cached routes for the application.

loadRoutes

This is a protected method that loads the application routes. It calls the global callback if set, or the callback registered using routes(), or calls the map() method if it exists.

__call

This magic method is responsible for forwarding any dynamic method calls to the router instance.

<?php

namespace Illuminate\Foundation\Support\Providers;

use Closure;
use Illuminate\Contracts\Routing\UrlGenerator;
use Illuminate\Routing\Router;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Traits\ForwardsCalls;

/**
 * @mixin \Illuminate\Routing\Router
 */
class RouteServiceProvider extends ServiceProvider
{
    use ForwardsCalls;

    /**
     * The controller namespace for the application.
     *
     * @var string|null
     */
    protected $namespace;

    /**
     * The callback that should be used to load the application's routes.
     *
     * @var \Closure|null
     */
    protected $loadRoutesUsing;

    /**
     * The global callback that should be used to load the application's routes.
     *
     * @var \Closure|null
     */
    protected static $alwaysLoadRoutesUsing;

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        $this->booted(function () {
            $this->setRootControllerNamespace();

            if ($this->routesAreCached()) {
                $this->loadCachedRoutes();
            } else {
                $this->loadRoutes();

                $this->app->booted(function () {
                    $this->app['router']->getRoutes()->refreshNameLookups();
                    $this->app['router']->getRoutes()->refreshActionLookups();
                });
            }
        });
    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        //
    }

    /**
     * Register the callback that will be used to load the application's routes.
     *
     * @param  \Closure  $routesCallback
     * @return $this
     */
    protected function routes(Closure $routesCallback)
    {
        $this->loadRoutesUsing = $routesCallback;

        return $this;
    }

    /**
     * Register the callback that will be used to load the application's routes.
     *
     * @param  \Closure  $routesCallback
     * @return void
     */
    public static function loadRoutesUsing(Closure $routesCallback)
    {
        static::$alwaysLoadRoutesUsing = $routesCallback;
    }

    /**
     * Set the root controller namespace for the application.
     *
     * @return void
     */
    protected function setRootControllerNamespace()
    {
        if (! is_null($this->namespace)) {
            $this->app[UrlGenerator::class]->setRootControllerNamespace($this->namespace);
        }
    }

    /**
     * Determine if the application routes are cached.
     *
     * @return bool
     */
    protected function routesAreCached()
    {
        return $this->app->routesAreCached();
    }

    /**
     * Load the cached routes for the application.
     *
     * @return void
     */
    protected function loadCachedRoutes()
    {
        $this->app->booted(function () {
            require $this->app->getCachedRoutesPath();
        });
    }

    /**
     * Load the application routes.
     *
     * @return void
     */
    protected function loadRoutes()
    {
        if (! is_null(static::$alwaysLoadRoutesUsing)) {
            $this->app->call(static::$alwaysLoadRoutesUsing);
        } elseif (! is_null($this->loadRoutesUsing)) {
            $this->app->call($this->loadRoutesUsing);
        } elseif (method_exists($this, 'map')) {
            $this->app->call([$this, 'map']);
        }
    }

    /**
     * Pass dynamic methods onto the router instance.
     *
     * @param  string  $method
     * @param  array  $parameters
     * @return mixed
     */
    public function __call($method, $parameters)
    {
        return $this->forwardCallTo(
            $this->app->make(Router::class), $method, $parameters
        );
    }
}