How to use the var_dump output directly from the controller?

18,594

Solution 1

if you don't see output from controller check if in your template file you have this line:

{{ content() }}

you can use php's var_dump in any place of your code:

var_dump($var);exit;

exit; is to stop anything what happens after this line.

You can also dump your vars in volt's template with volt's function:

{{dump(var)}}

dump() is same as var_dump() Here are some more useful volt functions:

http://docs.phalconphp.com/en/latest/reference/volt.html#functions

Solution 2

There is an implicit rendering level in the controller, in the first view that is rendered, you must call the getContent() method:

<div class="controller-output"><?php echo $this->getContent(); ?></div>

Or in Volt:

{{ content() }}

Solution 3

Ok, thx twistedxtra for the tip!

In my case, I use Twig. To resolve my issue I've added a feature to Twig:

$function = new \Twig_SimpleFunction('content', function() use($view) {
    return $view->getContent();
});

$this->_twig->addFunction($function);

Now it can be used in templates:

{{ content()|raw }}

Solution 4

You can totally disable view in the action:

class IndexController extends \Phalcon\Mvc\Controller
{
    public function indexAction()
    {
        $custom = "Custom variable";
        $this->view->disable();
        var_dump($custom);

    }
}

Or even use own debug method:

class IndexController extends \Phalcon\Mvc\Controller
{
    public function indexAction()
    {
        $custom = "Custom variable";
        $this->debug($custom);

    }

    public function debug($data)
    {
        $this->view->disable();
        var_dump($data);
    }
}

Solution 5

I know I'm a little late, but, just call

exit;

after your var_dump()

Share:
18,594
Yury
Author by

Yury

Hello my name is Yury and I'm PHP backend developer. Now I'm studying Swift and React Native development. I worked in the largest IT companies in e-commerce projects for Europe and Russia. I have a lot of experience developing with the Symfony 3/4 framework. But lately I'm more interested in UI/UX. So I develop my iOS applications to get experience and continue my career in developing mobile applications.

Updated on July 27, 2022

Comments

  • Yury
    Yury almost 2 years
    class IndexController extends \Phalcon\Mvc\Controller
    {
        public function indexAction()
        {
            $custom = "Custom variable";
            var_dump($custom);
        }
    }
    

    How to display the result not using the variables in the template?

    P.S. The result of the Echo function is also suppressed. I understand that this is the wrong approach, but it is a quick way to debug the variables.

  • Yury
    Yury over 11 years
    Perhaps you do not understand. It is about Phalcon framework. And I want to be able to debug variables through var_dump. Your method does not quite fit. Using die() function is a bad practice.
  • Yury
    Yury over 11 years
    But what if I use a different engine? For example Twig.
  • Oliver Bayes-Shelton
    Oliver Bayes-Shelton about 10 years
    Why couldn't he just use an exit; in his controller ?
  • Oliver Bayes-Shelton
    Oliver Bayes-Shelton about 10 years
    Not sure if anyone has seen this but it is by far the best way to do it, everyone else is giving examples which work with a templating engine.