'Laravel ServiceProvider vs. Middleware
I've got a question regarding Laravel. Where is the difference in using ServiceProviders
or Middleware
in Laravel?
So I mean, when do I use ServiceProviders
and when should I choose a Middleware
?
Both are executed on every request, don't they? Maybe someone of you have examples for use-cases.
Solution 1:[1]
There is a big difference between ServiceProviders and Middleware.
Service providers are the central place of all Laravel application bootstrapping. This can include your own application, as well as all of Laravel's core services. This is all bootstrapped via service providers. By bootstraping, I mean registering things, including registering service container bindings, event listeners, middleware
, and even routes. Service providers are the central place to configure your application. So, in short, Service providers are used to tell Laravel which classes and files should be loaded and used when you run your application. Service providers have two methods: register()
and boot()
.
Within the register
method, you should only bind things into the service container. You should never attempt to register any event listeners, routes, or any other piece of functionality within the register method. Simple example would be registering your custom class:
public function register()
{
$this->app->bind('App\Library\Services\CustomClass ', function ($app) {
return new CustomClass ();
});
}
We've imported App\Library\Services\CustomClass so that we can use it. In the register method, we've used the bind method of the service container to add our service container binding. So, whenever the App\Library\Services\CustomClass dependency needs to be resolved, it'll call the closure function, and it instantiates and returns the App\Library\Services\CustomClass object.
The boot
method is called after all other service providers have been registered, meaning you have access to all other services that have been registered by the framework. In most cases, you want to register your event listeners in this method, which will be triggered when something happens, and so on.. One simple example could be extending Laravel's validation to add some custom fields of your own:
public function boot()
{
Validator::extend('my_custom_validator', function ($attribute, $value, $parameters, $validator) {
// validation logic goes here...
}
More info about Service providers could be found on official documentation.
On the other side, middleware
provides a convenient mechanism for filtering HTTP requests entering your application. For example, Laravel includes a middleware that verifies the user of your application is authenticated. If the user is not authenticated, the middleware will redirect the user to the login screen. However, if the user is authenticated, the middleware will allow the request to proceed further into the application:
public function handle($request, Closure $next)
{
if (Auth::check()) {
// The user is logged in, do something...
}else{
// The user is not logged in, do something else...
}
More info about middleware could be found in official documentation.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | zlatan |