Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel - AppServiceProvider is not called

Tags:

laravel

I tried to bind some services in the Laravel AppServiceProvider, but the services weren't bound. I think, that the AppServiceProvider was not even called. Actually, I made a new one and it works.

So my question is, am I doing something wrong? Or was the AppServiceProvider not called?

like image 1000
Tadeáš Jílek Avatar asked Feb 23 '15 14:02

Tadeáš Jílek


People also ask

Where is AppServiceProvider php in Laravel?

Default Laravel Service Providers. Let's start with the default service providers included in Laravel, they are all in the app/Providers folder: AppServiceProvider.

How do I register as a provider in Laravel?

Registering ProvidersAll service providers are registered in the config/app. php configuration file. This file contains a providers array where you can list the class names of your service providers. By default, a set of Laravel core service providers are listed in this array.

What is service provider in Laravel 8?

Service providers can be defined as the central place to configure all the entire Laravel applications. Applications, as well as Laravel's core services, are bootstrapped via service providers. These are powerful tools for maintaining class dependencies and performing dependency injection.

What is route service provider in Laravel?

All Laravel routes are defined in your route files, which are located in the routes directory. These files are automatically loaded by your application's App\Providers\RouteServiceProvider . The routes/web.php file defines routes that are for your web interface.


1 Answers

Laravel pre-compiles certain classes that are used on basically every request. This serves the purpose of performance optimization. Files to compile can be specified in config/compile.php under files. The default one looks like this:

'files' => [
    realpath(__DIR__.'/../app/Providers/AppServiceProvider.php'),
    realpath(__DIR__.'/../app/Providers/BusServiceProvider.php'),
    realpath(__DIR__.'/../app/Providers/ConfigServiceProvider.php'),
    realpath(__DIR__.'/../app/Providers/EventServiceProvider.php'),
    realpath(__DIR__.'/../app/Providers/RouteServiceProvider.php'),
],

When running php artisan optimize when debugging is not enabled (or with the --force option) Those listed files and other framework classes will be written to storage/framework/compiled.php.

That means if you change one of those precompiled files, changes won't be applied immediately (if compiled.php exists) but only after you run php artisan optimize again or after you run php artisan clear-compiled to clear the compiled.php file.

Of course you can also remove the AppServiceProvider from the list as an alternative solution.

like image 66
lukasgeiter Avatar answered Sep 23 '22 19:09

lukasgeiter