Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confused - AppServiceProvider.php versus app.php

Where do I specify my bindings exactly? It seems I can do it in either of these files.

config/app.php Inside of 'providers' =>

app/Providers/AppServiceProvider.php Inside of register()

like image 477
user391986 Avatar asked May 21 '15 13:05

user391986


People also ask

What is AppServiceProvider Laravel?

The AppServiceProvider is the ability for us to provide services to the application. Inside of this file, there's two separate methods, register and boot. Register is used to register those classes that you want to make available inside of your Laravel application that are not dependent upon any other classes.

Why we use providers in Laravel?

Service providers are the central place to configure your application. If you open the config/app. php file included with Laravel, you will see a providers array. These are all of the service provider classes that will be loaded for your application.

What is the register and boot method in the service provider class?

Providers have two lifecycle methods: register and boot . The register method is where you should add bindings to the service container. The boot method is for performing actions after all service providers have registered their services.


1 Answers

The service providers array is loaded via config/app.php. This is the only actual place that providers are registered, and is where you should put Service Providers.

AppServiceProvider is for Laravel-specific services that you've overridden (or actually specified), such as the Illuminate\Contracts\Auth\Registrar, The HTTP/Console Kernels, and anything you wish to override in Laravel. This is a single service provider, that registers container bindings that you specify.

Really, you could load anything you want here, but there's a bunch of ready-to-go service providers in the app/Providers directory for you so you don't have to go and make one yourself.

like image 187
Amelia Avatar answered Sep 30 '22 15:09

Amelia