master

laravel/framework

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

FormRequestServiceProvider.php

TLDR

This file is a part of the Illuminate/Foundation/Providers namespace in the Demo Projects project. It contains the FormRequestServiceProvider class which extends the ServiceProvider class. The file defines the register and boot methods for the service provider.

Methods

register

The register method is responsible for registering the service provider. In this file, the register method is empty and does not contain any logic.

boot

The boot method is responsible for bootstrapping the application services. In this file, the boot method adds two callbacks to the application container. The first callback is added to the afterResolving method and it calls the validateResolved method on the resolved instance of ValidatesWhenResolved. The second callback is added to the resolving method and it creates an instance of FormRequest by calling the createFrom method with the current request and the specified form request class. It then sets the container and redirector on the created FormRequest instance.

Classes

FormRequestServiceProvider

The FormRequestServiceProvider class extends the ServiceProvider class and is responsible for registering and bootstrapping the form request services in the application.

<?php

namespace Illuminate\Foundation\Providers;

use Illuminate\Contracts\Validation\ValidatesWhenResolved;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Routing\Redirector;
use Illuminate\Support\ServiceProvider;

class FormRequestServiceProvider extends ServiceProvider
{
    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register()
    {
        //
    }

    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
        $this->app->afterResolving(ValidatesWhenResolved::class, function ($resolved) {
            $resolved->validateResolved();
        });

        $this->app->resolving(FormRequest::class, function ($request, $app) {
            $request = FormRequest::createFrom($app['request'], $request);

            $request->setContainer($app)->setRedirector($app->make(Redirector::class));
        });
    }
}