Laravel DebugBar
- Tram Ho
1. Introduction
A common problem for developers is encountering bugs and fixing bugs. As for fixing bugs, of course it’s never easy and debugging is the best way to help you check and fix bugs in your code. Today, I would like to introduce to you the debugging tool Laravel Debugbar, An efficient and convenient debugging tool.
Laravel Debugbar is a package built by Barry vd. Heuvel, allows you to quickly and easily debug while the browsing window is open. After installation Laravel Debugbar, On every web page we will see a toolbar at the bottom, it contains a lot of useful information for application error checking.
Laravel Debugbar has been updated for Laravel 5 with outstanding features such as:
- QueryCollection: Show all queries to the database
- RouteCollector: Show information of current route
- ViewCollector: Show views uploaded during that time
- EventsCollector: Show all events
- LaravelCollector: Tell us the version of Laravel and the application development environment
- ConfigCollector: Show values from config . file
2. Install Laravel Debugbar
I can install Laravel Debugbar into the Laravel project via composer:
1 2 | <span class="token function">composer</span> require barryvdh/laravel-debugbar --dev |
Next, open the config/app.php file in the providers array and add:
1 2 | Barryvdh\Debugbar\ServiceProvider::class<span class="token punctuation">,</span> |
Next if you want to add Facade, in the aliases array add:
1 2 | <span class="token string single-quoted-string">'Debugbar'</span> <span class="token operator">=></span> <span class="token class-name class-name-fully-qualified static-context">Barryvdh<span class="token punctuation">\</span>Debugbar<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Debugbar</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span> |
Public config file with the command:
1 2 | php artisan vendor<span class="token punctuation">:</span>publish <span class="token operator">--</span>provider<span class="token operator">=</span><span class="token string">"Barryvdh\Debugbar\ServiceProvider"</span> |
So that’s the installation step, now every time you run the project in debug mode, the debug bar will automatically load and display the parameters of the page you are viewing.
3. Learn about Debugbar
After successful installation, we can already use all the default features of DebugBar:
Messages
A special tab that is only loaded when you call the Facade Debugbar in your code.
1 2 3 4 | Debugbar<span class="token operator">:</span><span class="token operator">:</span><span class="token function">info</span><span class="token punctuation">(</span>$object<span class="token punctuation">)</span><span class="token punctuation">;</span> Debugbar<span class="token operator">:</span><span class="token operator">:</span><span class="token function">error</span><span class="token punctuation">(</span><span class="token string">'Error!'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> Debugbar<span class="token operator">:</span><span class="token operator">:</span><span class="token function">warning</span><span class="token punctuation">(</span><span class="token string">'Watch out…'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> Debugbar<span class="token operator">:</span><span class="token operator">:</span><span class="token function">addMessage</span><span class="token punctuation">(</span><span class="token string">'Another message'</span><span class="token punctuation">,</span> <span class="token string">'mylabel'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> |
Timeline
The perfect tool to help us fix slow, time-consuming code
1 2 3 4 5 6 | <span class="token class-name">Debugbar</span><span class="token double-colon punctuation">::</span><span class="token function">startMeasure</span><span class="token punctuation">(</span><span class="token string">'render'</span><span class="token punctuation">,</span><span class="token string">'Time for rendering'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">Debugbar</span><span class="token double-colon punctuation">::</span><span class="token function">stopMeasure</span><span class="token punctuation">(</span><span class="token string">'render'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">Debugbar</span><span class="token double-colon punctuation">::</span><span class="token function">addMeasure</span><span class="token punctuation">(</span><span class="token string">'now'</span><span class="token punctuation">,</span> LARAVEL_START<span class="token punctuation">,</span> <span class="token function">microtime</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">Debugbar</span><span class="token double-colon punctuation">::</span><span class="token function">measure</span><span class="token punctuation">(</span><span class="token string">'My long operation'</span><span class="token punctuation">,</span> <span class="token function">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// Do something…</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span> |
Exceptions
A tab that stores exception logs. You can log an exception to the debugbar using code that looks like this:
1 2 3 4 5 | <span class="token keyword">try</span> <span class="token punctuation">{</span> <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">Exception</span><span class="token punctuation">(</span><span class="token string">'foobar'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span>Exception $e<span class="token punctuation">)</span> <span class="token punctuation">{</span> Debugbar<span class="token operator">:</span><span class="token operator">:</span><span class="token function">addException</span><span class="token punctuation">(</span>$e<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> |
Views
The tab will show all that the template renders as well as all the parameters passed to them. It is really handy for application development. With views, we can be sure that what we pass in is exactly what the template is looking for. In addition, view has many other use cases
Route
You can see everything related to the route that was called, from the URI, middleware, controller, namespace, to the file path. This makes it easier to check the path of a request.
Queries
Queries are one of the most important parts of a web application. The Queries tab will show you all the queries that were used to display the web page you are viewing. This gives you a more visual view of the queries you use and makes editing easier.
Mail and Request
These two tabs include information you need to know about outgoing email and current requests.
4. Summary
In this article, I show you the basic features that Laravel Debugbar brings. You can experience and discover more features of Laravel Debugbar for yourself. Also, if you want to learn more, you can visit docs page of the Laravel Debugbar.