How to debug using dump and xdebug?

I was absent from Grav for some time and wanted to continue develoment. About two or three years ago I could simply place a dump() command inside a PHP file and saw the content in the browser. This is not the case any more. I see nothing. Any ideas?

I tried it on a hosted site where it worked in the past as well as on a fresh local installation on my laptop. It runs Arch with php7-apache, php7-fpm, php7-gd using built-in symfony.

1 Like

Thanks for providing all that environment info. :star: More posters should do this!

Pretty sure this PHP command is still supported. The only reasons I can imagine that it might not render are:

  • CSS or something else in the page layout is hiding the dump output
  • the PHP code is being skipped (for example, within an if block that is evaluated false)

Take a look at the chapter on other available debug techniques as well, sometimes there are better options than PHP dump().

Let us know how you go, please.

Thanks for your reply. I have a fresh install of grav-admin and added the events plugin. To the plugin I added the following code:

	public function onAdminSave(Event $event)
	{
		$this->grav['log']->info('Entering onAdminSave');

		$config = (array) $this->config->get('plugins.events');
		dump($config);
		$this->grav['log']->info('onAdminSave dump finished');
        ....

This is the only change I made after installation and it leads to

[2022-11-09 21:31:10] grav.INFO: Entering onAdminSave [] []
[2022-11-09 21:31:10] grav.INFO: onAdminSave dump finished [] []

in grav.log. But I see nothing from the dump.

So it looks like that code block is being run. Since this is part of Admin and that has lots of tricky JS and CSS and AJAX, I think it might be the first option I suggested.

Sometimes I have added die(); after the dump() when I suspect my dump output is being obscured.

Thank you very much for that hint. When I add die(); I can see the dump. It’s not very comfortable, but it will help.

What about logging variable contents to log file or debugbar? The documentation didn’t help me much, as the PHP examples only output some textstrings. How do I use it for more complicated PHP types?

I don’t know, but here are some suggestions:

  • remove die() and view the page source (it’s obscured in rendering but should be in there somewhere); browser ‘Inspect’ might show something semi-readable too
  • enclose your ugly dumped output before die() with <pre>...</pre>
  • use the other debugger “clockwork” - I understand that it’s much better (you’d need to use $this->grav['debugger']->addMessage($myvariable) from PHP)
  • try Xdebug if it’s available - have only just started with this and have not got everything working yet.

The reason why dump() doesn’t show during onAdminSave is because the Save action is performed by multiple task requests to the server.

When hitting Save on a page:

  • The first request: onAdminSave is being called, but there is no call to onOutputGenerated.
  • In subsequent requests, onAdminSave is not being called, and onOutputGenerated is being called in some of them.

Conclusions:

  • So I guess that any output generated using dump() during onAdminSave will never be sent to the client.
  • dump() does work in events like OnPluginInitialized, onPageInitialized, …

Just curious, why would one use dump() when one can step through code using a debugger?

	public function onAdminAfterSave(Event $event)
	{
		$this->grav['debugger']->addMessage("onAdminAfterSave");

		$icalendar = new \Events\iCalendarProcessor();
		$this->grav['debugger']->addMessage($icalendar);

doesn’t show anything in the debugbar. What am I doing wrong?

Because one didn’t know that it’s possible :see_no_evil:
Are you talking about Xdebug, too?

@myscha,

Because one didn’t know that it’s possible :see_no_evil:

Uh… wasn’t that part of course Programming 101 ? :wink:

Are you talking about Xdebug, too?

Yes. See Xdebug docs.

Here is a screenshot when using Xdebug in VScode while hovering over parameter $event after a breakpoint inside onAdminSave (line 88) has been hit.

And this is the list of variables shown in the left sidebar of VScode

I assume you use a/some VScode plugin/s, can you tell which one/s for PHP?

1 Like

I would also like to know this.

Plenty of tutorials can be found on the web…

  • What have you tried sofar? Which steps?
  • Where in the process of setting up Xdebug with VSCode did you get stuck?

In short:

  • Install Xdebug on your system and update Apaches PHP ini file.
  • Install Xdebug Helper for Chrome or Firefox.
    • Browse to the website you wish to debug, open extension and switch on “Debug”.
  • In VSCode:
    • Install VSCode extension:
      • PHP Debug by Xdebug
    • While you’re adding extensions, add as a bonus for PHP editing:
      • PHP Intelephense by Ben Mewburn
      • Twig Language 2 by mblode
    • From the left menu hit “Run and Debug” icon. Then, in the sidebar:
      • Click “Create a launch.json file”
      • Chose “PHP” from dropdown.
      • Start the debugger using the green arrow

Thanks. I doubt you can help in my situation, but here’s why I think I’m having trouble in a nutshell:

  • I’m accessing PHP in docker containers from VSCode on the host. I have Xdebug info output and can dump but not step debug. The step debugger is enabled but unable to connect to the configured “client”. Online guides tell me not to open a port on the docker container, so I am assuming VSCode is the “server” here.
  • I am probably wasting time because I am confused which is server and which is client.
  • I am probably wasting time because I don’t understand whether I should set Xdebug up in the browser or VSCode or both. I’m trying both.
  • The VSCode config/launch UI is pretty confusing and the settings.json file is not exactly self documenting. I think my “path to PHP” setting might need a special value.

This is all a diversion from the original question and I don’t expect that you can help. I wanted to explain what I am finding difficult. Some online resources have helped me progress but there still seem to be significant gaps in my understanding of how this should work.

@hughbris,

I doubt you can help in my situation

I’ve never used Docker (I’m using WSL on Windows 11), so indeed I might not be of much help in you situation.

This is all a diversion from the original question

I’ve changed the title to: How to debug using dump and xdebug?

Might it be an idea to start getting XDebug running on a environment without Docker? Just to get a feel of how things interop. Once you’ve got that working, you can add the complexity of Docker.

Could this blog be of any help: How to develop PHP inside a Docker Container using VS Code | DEVSENSE Blog. It also shows Xdebug.

Thanks, it’s because I know your environment is nothing like mine that I doubted you could help.

It would confirm that Docker is the problem. However I try to use Docker for server functions to keep my desktop clean and I’m reluctant to lose that. It’s a double edged sword, I agree.

Thanks for your offers and the link but it’s not quite what I’m looking for.