1

I am trying to do a var dump from a Drupal 8 Controller class. This is not working because controllers need to send a response. I just need to see the contents of a variable and I have not idea how to do this since I've tried all response combinations and can't seem to get it working.

This is my variable.

$storage = \Drupal::entityTypeManager()->getStorage('node');
$node = $storage->load($rep_id);

I am trying to var_dump($node) to see what's in it. Can anyone help?

2

You should be able to do this:

use Symfony\Component\HttpFoundation\Response;

return new Response('<pre>' . var_export($node, TRUE) . '</pre>');
3

OK, I'm going to be that guy. Do yourself a favour and set up Xdebug, hacks like var_dump() or var_export() are very bad practice and sooner or later you will forget to remove some of those statements and it will end up in production. Also it takes one single click to set up a breakpoint, as opposed to writing a var_dump() or var_export() statement. Not to mention that the amount of information you can get by using a debugger is vastly more comprehensive.

  • By that guy I assume you mean that sensible, less-stressed guy :) – Clive May 21 '17 at 13:27
  • Good advice. Admittedly, I'm still not great at debugging, but I'm definitely open to better methods. – garek007 May 21 '17 at 19:44
2

While there's a few options, you could try:

drupal_set_message('<pre>' . var_export($node, 1) . '</pre>');
1

These are some other techniques:

1] Debug

  • Drupal core comes with its own function debug()

2] Devel

  • intstall composer require symfony/var-dumper
  • clear cache with UI or drush cr
  • at devel settings page set var-dumper as a default option
  • use devel functions dpm() kpr() for inspecting variables in your code
  • {{ devel_dump() }}, {{ kpr() }} or {{ dpm() }} in twig templates

3] Other contributed modules

Note: (Read Jaypan's answer) You can also use PHP's native functions like var_dump or print_r

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.