Michael Dyrynda
Home Blog Podcasts
Using Laravel Telescope in specific environments October 29th, 2018

Announced at Laracon AU 2018, and released shortly therafter, Laravel Telescope is an elegant debugging assistant for the Laravel Framework, improving upon functionality available in Laravel Debugbar.

I won't talk much about Telescope here, but you can learn more from this post on Matt Stauffer's blog.

Whilst its primary purpose is as a debugging tool in development, it is also a powerful asset in debugging your production environment. In order to get Telescope, however, I ran in to some stumbling points in my CI environment in GitLab.

To work around this, I set out to conditionally load Telescope. I stumbled upon this comment from Mohamed Said, which suggested loading the TelescopeServiceProvider in one of your application's service providers, but doing so was not enough.

Telescope leverages package auto-discovery to load its default package provider, but in order to prevent this for certain environments (build, testing), we need to opt-out by including laravel/telescope in the composer.json file's extra.laravel.dont-discover key.

1"extra": {
2 "laravel": {
3 "dont-discover": [
4 "laravel/telescope"
5 ]
6 }

The default telescope:install command will also place an entry in config/app.php, which is used for application-specific configuration of Telescope, such as authorising access in non-local environments and enabling dark mode.

In order to fully control which conditions Telescope is loaded under, we must also remove the App\Providers\TelescopeServiceProvider::class entry from the providers array.

Now, in your AppServiceProvider's register method, we can add the following:

1if (! $this->app->environment('build', 'testing')) {
2 $this->app->register(\Laravel\Telescope\TelescopeServiceProvider::class);
3 $this->app->register(TelescopeServiceProvider::class);

This ensures that both the default Telescope registration, which would have been handled by package auto-discovery, occurs as well as any application-specific configuration in App\Providers\TelescopeServiceProvider.

I'm a real developer ™
Michael Dyrynda


I am a software developer specialising in PHP and the Laravel Framework, and a freelancer, blogger, and podcaster by night.

Proudly hosted with Vultr

Syntax highlighting by Torchlight