I use several packages in my project which are only useful in a development environment (e.g. generators, debug bar,..). Recently, development has been completed and I would like to deploy the app to a production environment on Fortrabbit. Sadly, I'm running into issues when deploying my app:
remote: -> Generating autoload files
remote: ! Composer threw 'RuntimeException'
remote: ! Error Output: PHP Fatal error: Class 'Barryvdh\Debugbar\ServiceProvider' not found in /var/www/web/obelusk/htdocs/vendor/laravel/framework/src/Illuminate/Foundation/ProviderRepository.php on line 150
I know what the error means, what I would like to know is how to fix it. How can I make sure that my app does or does not load these service providers based on the environment?
You should omit the service provider of packages you want to load conditionally from the providers array, and rather use AppServiceProvider and its register() method to conditionally register service providers, like so:
// AppServiceProvider.php
public function register()
{
if ($this->app->environment('local')) {
$this->app->register('Barryvdh\Debugbar\ServiceProvider');
}
}
@toniperic Very helpful indeed, I assumed something like this was the solution. How do you handle facades that come with these service providers though?
// AppServiceProvider.php
public function register()
{
if ($this->app->environment('local')) {
// register the service provider
$this->app->register('Barryvdh\Debugbar\ServiceProvider');
// register an alias
$this->app->booting(function()
{
$loader = \Illuminate\Foundation\AliasLoader::getInstance();
$loader->alias('Debugbar', 'Barryvdh\Debugbar\Facade');
});
}
}
@toniperic I'm running into reflection exceptions where some classes are not found, even though I did manually run the composer dump-autoload command several times. Not that it should matter, since these classes are in the App namespace and should be loaded via PSR4 autoloading...
@Elimentz can you be more precise about the errors?
I am wondering why is this happening because it shouldn't at all when the if-statement doesn't pass. And if your environment is not local, then it just simply shouldn't ever try to register the server provider and alias.
@toniperic Not really, there really isn't much more to say... This is the error I get:
21:06:10 yourApp@fortrabbit:~/htdocs$ php artisan list -vvv
[ReflectionException]
Class App\Console\Commands\UpdateInformationCommand does not exist
I checked, the file exists, the namespace is set correctly and the command works on my development environment. Even more bizarre, the app itself seems to function perfectly so it seems like it's just the CLI that is experiencing some issues.
@toniperic Found the issue - as it turns out, my Ubuntu box doesn't really case about case-sensitivity while the Fortrabbit server does. Not sure how that's possible but renaming the file into the proper case solved it for me.