9
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
  • 908
  • 4
  • 12
  • 26

9 Answers9

13

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

Lukas Liesis
  • 24,652
  • 10
  • 111
  • 109
5

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() }}
twistedxtra
  • 2,699
  • 1
  • 18
  • 13
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 }}
Community
  • 1
  • 1
Yury
  • 908
  • 4
  • 12
  • 26
2

Based on your above code , i understand that your required to execute the $custom value. There are 2 ways as follow

1 - You can write var_dump($custom); and after that put die(); so that after it no code can be executed.

  1. You can write echo $custom , for execute the value of $custom. But you have to stop script execution after it.

May be some times it happen that code as been written but due to template or view file execution it will overwrite your code. You must check the source code does anything printed above tag that you have written in controller.

May this will help you........:)

Harsh Chunara
  • 585
  • 3
  • 10
  • 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 Jan 26 '13 at 10:31
2

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

exit;

after your var_dump()

brian
  • 2,745
  • 2
  • 17
  • 33
2

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);
    }
}
barell
  • 1,470
  • 13
  • 19
0

Phalcon\Mvc\View\Engine\Twig() change to:

https://gist.github.com/4690638

and use:

{{condent()|raw}}
{{linkTo('#', 'title')|raw}}

This is my fork ;-)

Load
  • 1
0

An even simpler approach would be to set "$this->view->disable()" just above your var_dump expression when using volt. Maybe this would also work with other template engines.

dompie
  • 711
  • 1
  • 7
  • 12
0

Why not use good old

echo "<pre>" . print_r($custom,TRUE) . "</pre>";

Prints nice and ordered array. Have to add it works from both Controllers and Views. In case of Controller, output is placed on top of Controller's view.

s3r3k
  • 91
  • 1
  • 4