how-to-use-laravel-debugbar-in-production

How to use Laravel Debugbar in Production

In this article, we are going to show you how you can use a Laravel debug bar library in your live project. Debug Bar is used mainly for debugging environments but there can be situations where you might face an error in production. But debug bar displays all the queries and models of your Laravel application. So you do not want to show it to all of your live users. It should only be displayed to you, the owner of the website.

What we will do:

  1. Install Laravel
  2. Install Debug bar
  3. Enable for specific user only

Video Tutorial:

Install Laravel

First, we are going to install a fresh Laravel project. You can use it in your existing project as well. Open the command prompt or terminal at the root of your Laravel project. Download Laravel from Github, you need to extract the files. Copy and paste all the files in your htdocs project folder, make sure to copy the hidden files as well.

Then create a copy of the .env.example file and rename it to just .env. Then move all the files from the public folder to the root folder. At this point you can delete the folder downloaded from GitHub, it is not needed anymore.

.env

Now open the .env file and set the app debug to false and app environment to “production” because we will be going into production.

APP_ENV=production
APP_DEBUG=false

Then we will set up the MySQL configurations, we are not going to use MySQL but it is required to run the Laravel project.

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=8889
DB_DATABASE=test
DB_USERNAME=root
DB_PASSWORD=root

You can enter as per your MySQL server details.

index.php

Then we need to open the index.php file and do the following changes in it:

// change
if (file_exists(__DIR__.'/../storage/framework/maintenance.php')) {
    require __DIR__.'/../storage/framework/maintenance.php';
}
// to
if (file_exists(__DIR__.'/storage/framework/maintenance.php')) {
    require __DIR__.'/storage/framework/maintenance.php';
}

// change
require __DIR__.'/../vendor/autoload.php';
// to
require __DIR__.'/vendor/autoload.php';

// change
$app = require_once __DIR__.'/../bootstrap/app.php';
// to
$app = require_once __DIR__.'/bootstrap/app.php';

Then we need to run a command in our terminal to install the Laravel:

COMPOSER_MEMORY_LIMIT=-1 composer update

COMPOSER_MEMORY_LIMIT=-1 is prepended before the command because sometimes we may face an error that says “allocated memory exhausted”. The command will take a few minutes.

Once it is installed, you need to generate an application key which you can generate with the command:

php artisan key:generate

You can see your application key in the .env file.

APP_KEY=base64:xChDAqJJXU70jlLo4eqcsCr53Lnw+MVqqus3Sx8Nwfw=

Now open your project in the browser, you will see that it has been installed perfectly and is running. Now we need to install debug bar in it.

Install Debug bar

Download debug bar library (Github) by running the following command in your terminal:

COMPOSER_MEMORY_LIMIT=-1 composer require barryvdh/laravel-debugbar

This will take a few minutes but once installed, you can refresh your browser. The debug bar will not be displayed because the app is in production, so we have to manually display it. But we also want it to be displayed on all pages, throughout the application.

So we will go to the app > providers > AppServiceProvider.php and update it as per the following code:

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

use Auth;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        if (Auth::check() && Auth::user()->email == "your_email@gmail.com")
        {
            \Debugbar::enable();
        }
        else
        {
            \Debugbar::disable();
        }
    }
}

Make sure to add the backslash before it, otherwise, it will generate an error because Laravel works in namespaces. Enter your email address in the if condition. Now you will start seeing a debug bar in your production but only when you are logged in with the above-provided email address.

Conclusion

So that was all, if you face any problem in following this, kindly do let me know in the comments section below.



Leave a Reply

Please disable your adblocker or whitelist this site!