What is the use of service providers for laravel

Hi guys, In this article, We are studying Service Providers. It is the central place of all laravel application bootstrapping. You can be bootstrapping custom classes and laravel core classes, which contain registering service container bindings, event listeners, middleware, and custom routes. You can see providers inside the config/app.php file. It contains the Providers array. You can specify your service providers and already contain a set of laravel core service providers and Provider loads on a specific route. You will learn how to write and register your service provider in the laravel application. Create a Service Provider All Service Provider extends the Illuminate\Support\ServiceProvider class. Run the below command to make your service provider. php artisan make:provider InstallServiceProvider It contains a register and boot method. In the register method, you can specify only bind things into the service container. You should not register any event listeners or routes. Register method: You should not register any event listeners, routes, or functionality within the register method of a service container. Instead, only bind things in the container. You can access the $app property, which provides access to the service container.

Mar 25, 2024 - 12:30
 0
What is the use of service providers for laravel

Image description

Hi guys,

In this article, We are studying Service Providers. It is the central place of all laravel application bootstrapping. You can be bootstrapping custom classes and laravel core classes, which contain registering service container bindings, event listeners, middleware, and custom routes.

You can see providers inside the config/app.php file. It contains the Providers array. You can specify your service providers and already contain a set of laravel core service providers and Provider loads on a specific route. You will learn how to write and register your service provider in the laravel application.

Create a Service Provider

All Service Provider extends the Illuminate\Support\ServiceProvider class. Run the below command to make your service provider.

php artisan make:provider InstallServiceProvider

It contains a register and boot method. In the register method, you can specify only bind things into the service container. You should not register any event listeners or routes.

Register method:

You should not register any event listeners, routes, or functionality within the register method of a service container. Instead, only bind things in the container. You can access the $app property, which provides access to the service container.

app->singleton(Connection::class, function ($app) {
            return new Connection(config('riak'));
        });
    }
}

Bindings and Singletons Properties:

You want to register many service providers in a simple binding using bindings and singletons properties. It will automatically check these properties and register their bindings.

 DigitalOceanServerProvider::class,
    ];

    /**
     * All of the container singletons that should be registered.
     *
     * @var array
     */
    public $singletons = [
        DowntimeNotifier::class => PingdomDowntimeNotifier::class,
        ServerProvider::class => ServerToolsProvider::class,
    ];
}

Click here to Read More

What's Your Reaction?

like

dislike

love

funny

angry

sad

wow

Viral News Code whisperer by profession, narrative alchemist by passion. With 6 years of tech expertise under my belt, I bring a unique blend of logic and imagination to ViralNews360. Expect everything from tech explainers that melt your brain (but not your circuits) to heartwarming tales that tug at your heartstrings. Come on in, the virtual coffee's always brewing!