Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ixudra's avatar

Production vs dev config

Hi

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?

0 likes
13 replies
toniperic's avatar

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');
    }
}

Hope that helps.

ixudra's avatar

@toniperic Very helpful indeed, I assumed something like this was the solution. How do you handle facades that come with these service providers though?

toniperic's avatar
Level 30

@Elimentz same thing.

// 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');
        });
    }

}
pixelpeter's avatar

Keep in mind that you can break artisanwith this approach (from my experience)

I did the same conditional loading with Jeffreys Generator and after this was unable to execute artisan commands on the live system

ixudra's avatar

@pixelpeter Indeed, I'm running into issues myself too now... Did you manage to solve them? And if so, how did you do it?

pixelpeter's avatar

Well if I need it on my live system it's not a dev requirement ;-) So I moved the generators from require-dev to the standard require section

1 like
ixudra's avatar

@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...

toniperic's avatar

@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.

ixudra's avatar

@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.

ixudra's avatar

@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.

toniperic's avatar

There you go. It didn't made sense that it just breaks out of nowhere. Glad it's actually working.

Cheers!

Please or to participate in this conversation.