How to debug Laravel framework?
Asked Answered
D

7

30

I'm a newbie PHP programmer now casting an eye at frameworks, and among them Laravel sounds appealing to me as a beginner (somehow unfortunately because I've already invested in literature on Zend 2). However, debugging is important to me and from what I could find on the net, Laravel is hard to debug. I currently use Xdebug in Netbeans on native PHP code and wonder whether Laravel can be debugged in the same or a similar way? When I open a new Netbeans project I can select Symfony 2 or Zend 2, but there seems to be indeed no support for other frameworks like Laravel? If not, is there at least an equivalent debugging tool?

Drucilladrucy answered 29/5, 2014 at 6:57 Comment(2)
matthewhailwood.co.nz/… would help in some way. :)Helio
xdebug doesn't require an IDE, and can be used with any PHP code, so using NetBeans doesn't prevent you using it in any way even though it doesn't have a specific menu optionNeglect
I
24

Laravel has its own debugging system.You can use built in dd() function.And there are several packages that can be used to debug laravel projects.Here are some links and hope that it will be helpful for you.

https://github.com/barryvdh/laravel-debugbar

http://laravel.io/forum/02-04-2014-package-laravel-debugbar

Immunotherapy answered 29/5, 2014 at 8:53 Comment(2)
Can you expand your answer a bit more on dd()? When I use it, all I see in the browser is ^ "App\Http\Kernel"Bowes
dd(foo) outputs the variable and then ends the script (dump and die). Your browser is showing nothing probably because you are not dumping any data to it. laravel.com/docs/8.x/helpers#method-ddImpost
Y
6

Recently I came to discover this amazing plugin that allows you to dump variables, trace requests, executions, views, controllers, queries, profile memory, execution time, etc., everything related to the current rendered page. Very helpful :

https://laravel-news.com/laravel-debugbar

You can install it via composer:

composer require barryvdh/laravel-debugbar --dev

Then add it to your service providers array in /config/app.php

The Debugbar will start working inmediately if the debug mode is turned on: To do it so, you just need to modify in your config/app.php or .env file the debug_mode to true.

If you wish to use the dump methods in the debugbar console, you need to include the alias to your /config/app.php array:

'Debugbar' => Barryvdh\Debugbar\Facade::class,

Now you can start dumping variables like this:

\Debugbar::info($variable);

Pretty cool plugin. Cheers!

Yearning answered 2/3, 2018 at 16:16 Comment(0)
S
4

laravel telescope

https://github.com/laravel/telescope

in laracast you can find even a episodie about it

https://laracasts.com/series/laravel-from-scratch-2018/episodes/28

Simplistic answered 20/8, 2019 at 19:37 Comment(0)
P
3

Also there is Google Chrome extension "PHP Console" service provider for Laravel https://github.com/barbushin/php-console-laravel

Pugilist answered 18/9, 2014 at 20:45 Comment(0)
I
1

At first install this package:

composer require barryvdh/laravel-debugbar --dev

In config/app.php Add Inside providers array:

Barryvdh\Debugbar\ServiceProvider::class,

And then aliases array:

'Debugbar' => Barryvdh\Debugbar\Facade::class,

After that you can debugging by:

Debugbar::info($object);
Debugbar::error('Error!');
Debugbar::warning('Watch out…');
Debugbar::addMessage('Another message', 'mylabel');
Ichthyology answered 1/8, 2021 at 18:0 Comment(0)
P
1

Quite a deep topic but let me breakdown debugging based on my 3yr experience :)

First of all, different cases require different solutions, here is bullet-list how I do debugging:

Xdebug - great way to debug PHP code on local machine but requires some time to configure. Handy tool for beginners, but rarely saw it used by experienced developers.

LaravelDebugbar - used in almost all of our projects (mainly to optimize queries) on both staging and production servers.

LaravelTelescope - debugging dashboard.

Spatie Ray - paid desktop app that helps you debug your app.

Sentry - dashboard for app exceptions visualization and management.

dd() - returns 500, dumps the variable, and dies. Generally good for solo development because otherwise may interfere with college work. There are few implementations of dd() inside Laravel classes e.g Http:dd(), $collection->dd(), and so on.

dump() - dumps the variable (same way as dd()) but script continues. May be useful when need to echo variables without losing the actual response from server. Caution: may brake frontend.

Log::info($string, $array) - log variables into .log file. Used a lot when doing hotfixes on live servers or long logic. We always have separate channel for such cases configured at config/logging.php:

dev' => [
  'driver' => 'single',
  'path' => storage_path('logs/dev.log'),
],

storage/logs/laravel.log itself - if we are talking about logic exceptions, do not forget about built-in exceptions logger, here you can find time and trace with exact file and line where error has been thrown. Advise: change default channel driver to 'daily' for default logger.

In addition, we have helper functions. isdev() - mainly for live server fixes, helps call test\debug logic for developers without breaking end-user experience:

function isdev() {
    try {
        if (isset($_GET['some-anchor-to-define-developer'])) { //! not secure
            return true;
        }

        $ips = [
            '127.0.0.1',
            //
        ];

        // any logic to define developer, e.g. check auth()->id()

        return in_array(request()->ip(), $ips);
    } catch (\Throwable $th) {
        return false;
    }
}

in config/app.php we have this line:

'debug' => isdev() ? true : (bool) env('APP_DEBUG', false),

dlog() - just a shortcut for var logging described above:

function dlog(string $text, array $array=[]) {
    \Log::channel('dev')->info($text, $array);
}

Besides this, there are few other PHP\Laravel functions which you may consider useful: var_dump(), print_r(), die(), exit(), microtome(), memory_get_usage(), \DB::connection()->enableQueryLog() + \DB::getQueryLog(), $builder->toSql(), dumpRawSql() and ddRawSql() (v10), benchmark class (v10).

In addition, on all projects, we have a special DevController\Command where we may call subset logic safely or just as a playground. It contains a few helper methods e.g. to get SQL query, to dump vars, and to calculate script execution time and memory.

To summarize. As you can see, there are quite a few debug solutions. So this is really up to the situation and personal preference which type of debugging to use. I am sure there are a lot more ready-ish solutions, so may advise you to search for a specific code that solves your exact debug request.

Punnet answered 15/10, 2023 at 12:59 Comment(0)
F
0

You can use the dephpugger. Is a debugger to run in terminal.

Is really simple to use and works like byebug in ruby. https://github.com/tacnoman/dephpugger

Fleuron answered 13/6, 2017 at 21:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.