First start by installing the two packages into your Laravel project.
composer require barryvdh/laravel-debugbar --dev
composer require hannesvdvreken/guzzle-debugbar --dev
Then add the service providers. As I only wanted to use the debugbar for local development I added them to the the AppServiceProvider instead of in the app.php config file.
// app/Providers/AppServiceProvider.php
public function register()
{
if($this->app->isLocal()) {
$this->app->register(\Barryvdh\Debugbar\ServiceProvider::class);
$this->app->register(\GuzzleHttp\Profiling\Debugbar\Support\Laravel\ServiceProvider::class);
}
}
You may also need to run composer update
if you are running an older version of Laravel 7 to get 7.18.
The below code shows an example controller that sends a get request to the jsonplaceholder api and gets an array of users. Following the instructions on the Guzzle Debugbar package and a bit of trial and error I managed to add the profiler middleware to the Laravel HTTP Client.
It took me a while to figure out how to get the results to display in the debugbar. Initially I was setting the $debugbar = new StandardDebugbar();
before I saw a closed issue on the package that explains you need to get the debugbar from the IOC container.
<?php
namespace App\Http\Controllers;
use GuzzleHttp\Profiling\Debugbar\Profiler;
use GuzzleHttp\Profiling\Middleware;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Http;
class ExampleController extends Controller
{
public function index()
{
$debugbar = App::make('debugbar');
$timeline = $debugbar->getCollector('time');
$profiler = new Profiler($timeline);
$users = Http::withOptions([
'base_uri' => 'https://jsonplaceholder.typicode.com/'
])
->withMiddleware(new Middleware($profiler))
->get('users')
->json();
return view('example', compact('users'));
}
}
When we load the view we now get the information about the request in the Timeline tab of the debugbar. The get request took 181ms.
#laravel #debugbar #http-client