laravel / octane

Supercharge your Laravel application's performance.
https://laravel.com/docs/octane
MIT License
3.73k stars 289 forks source link

dump() only works in cli #838

Closed AlliBalliBaba closed 4 months ago

AlliBalliBaba commented 5 months ago

Octane Version

2.23

Laravel Version

10.43

PHP Version

8.3.2

What server type are you using?

Swoole

Server Version

5.1

Database Driver & Version

No response

Description

I've noticed that using dump()or @dump does not work with Laravel Octane when we are dumping a response for a browser. The Symfony\Component\VarDumper\VarDumper seems to explicitly check if PHP_SAPI is 'cli' when dumping a variable. Since with Octane we're always in cli context, we're never using the HtmlDumper. I found out that this issue can be circumvented by explicitly setting the handler for the VarDumper like this when booting the app:

VarDumper::setHandler(function ($var, ?string $label = null) {
            $cloner = new VarCloner();
            $cloner->addCasters(ReflectionCaster::UNSET_CLOSURE_FILE_INFO);
            $var = $cloner->cloneVar($var);
            if (null !== $label) {
                $var = $var->withContext(['label' => $label]);
            }
            $dumper = app()->runningInConsole() ? new CliDumper() : new HtmlDumper();
            $dumper->dump($var);
        });

Ideally Octane would already handle this itself, unless I'm missing something.

Steps To Reproduce

Start an octane server. Create a route that renders a blade view. Use @dump($someVariable) in the blade view. dump() will display nothing.

Add the VarDumper::setHandler command from above into the boot() method of your AppServiceProvider. dump() will now properly display $someVariable in the browser

AlliBalliBaba commented 4 months ago

I created another pull request which is just a one-line fix for this issue. Would be cool if it gets merged. dump() not working when debugging an HTML response can create a lot of confusion since the behavior is different from regular Laravel.

driesvints commented 4 months ago

Thanks you. Let's see how the PR goes.

driesvints commented 4 months ago

Looks like we're not making any changes here at this time sorry.