How do service providers and service containers work using laravel?

Laravel can sometimes be very hard to deal with even for experienced developers as the core functionality is tough to understand. But indeed, web development is interesting. 

One of the functionalities is its Service Providers, on which I will give a detailed overview in this blog. I will also explain how a service container works in laravel in this blog. 

These are simpler than you might have imagined. Having said that, let’s get straight into it.

What are service providers in laravel?

The service providers in a Laravel application serve as the core point from which the application is bootstrapped. As a result, providers are used to inject laravel’s basic services into the service container as well as our application’s services, classes, and their dependencies into the service container.

New call-to-action

The provider for this example may be found in config/app.php as an array named “providers.” Refer to the image below:

What are service providers in laravel 1

Suppose you want to use any third party services provider, then you have to apply in the configuration of the service provider array.

You can see some service providers already there running the entire application, for example, Auth, which is required for authentication for the application.

How to create service providers in laravel?

To use a service provider, you must first create one. As a result, we’ll set up a service provider right now. You may use the command provided below to create any service provider you need.

php artisan make:provider MyServiceProvider 

When you run the command, a service provider will be created in your application, which you may then use. 

How to create service providers in laravel 1

After that, open your app and navigate to the providers folder, where you will locate your service provider.

How to create service providers in laravel 2

Here you will see two methods, register() and boot().

The register() method

It is very important to know the fact that the register() method allows us to define bindings to our service container. For instance, see code below:

The register method

$this->app is a global variable in laravel which can access a singleton class through the app.

Singleton is a trait. When you apply this trait, you are informing the application that whatever class is sent as a parameter in the app should only have one duplicate (instance) throughout the application. This means that MyClass will be resolved once and will have just one instance, which can be accessed using the my_class variable.

The boot() method

The boot() method allows you to access all of the services that were previously registered using the register method. You may thus include the entire service into your application by utilizing this method.

The boot method

The boot method-1

How to register a service provider?

As you have now built your service provider, you will need to register it in your configuration file to complete the process. You must register here in the manner depicted in the image below: 

How to register a service provider

All service providers will load first each and every time the application is run. Service providers are the main central place for Laravel applications, and they are always there to assist you in running the complete application, as previously discussed before in this blog. 

What are service containers in laravel?

The Service Container is a powerful mechanism that allows us to store all of the bindings that are necessary to execute a Laravel application on a single server.

6 Principles of modern web application development 2022

Everything in the application may be bound together with the assistance of a service container.

How to use service container in laravel?

Consider the following scenario: we have a class named MathService that is responsible for doing certain fundamental operations like addition, multiplication, and so on. The code for this class is provided below.

How to use service container in laravel

As you can see above, there are two methods for calculating the sum of numbers array using the array sum function and multiplying all of the numbers provided as an array using the array product function. 

The array sum function calculates the sum of numbers array using the array sum function, while the array product function multiplies all of the numbers provided as an array.

In order to use service provider, we must first bind the service to the service container.

How to use service container in laravel-1

However, by doing so, we are instructing Laravel to keep the MathService container in the container with the name MathService and to return it whenever we require an instance of a class. This is a one-time binding of a class, and it may be used to obtain any number of instances from our programme.

How to use service container in laravel-2As previously stated, service containers and providers are critical components of every Laravel application. As previously stated, we may design our own unique service providers or we can incorporate third-party service providers into our applications. 

As an example, if you want to utilize socialite in Laravel, you can just copy and paste it into the app service provider file and use it anywhere in your application.

Summing it up

When configuring your application, service providers are utilized. In addition to the service container bindings, other key elements such as routes, middleware, and event listeners are also registered in this section of the configuration file. 

The service providers are performed before the request itself is processed, ensuring that everything you want is in place and setup prior to the request being handled by the system. The execution of some service providers occurs on a regular basis while other service providers are only performed when they are required (“delayed providers”).

When you utilize a service container, you may use it for dependency injection (for example, parameter injection in your controller classes) as well as binding and resolution classes (like what class to instantiate for a given contract).

Web development can be a tough process. That is why webdew has a team of skilled web developers to help make websites and applications and make things easier for you. 

Need a hand with web design and web development? Get in touch with us!

                                                                                                                               Editor: Amrutha