3

I am working on Magento 2.

if ($this->getRequest()->isAjax()) {
    $layout =  $this->_layout;
}

How can I print or debug value of $layout in above code ??

1
  • echo $data = print_r($layout, TRUE); Commented Jul 19, 2019 at 13:16

5 Answers 5

3

On top of what Ashar Riaz said, you can also use the debug method from \Psr\Log\LoggerInterface :

$this->_logger->debug(var_export($layout, true));

IIRC, the values will be logged under var/log/debug.log

3

you can print $layout in log file like in below code and see results in var/log/debug.log file

class Example{
protected $_logger;
public function __construct(
    \Psr\Log\LoggerInterface $logger, //log injection
    array $data = []
) {
    $this->_logger = $logger;
    parent::__construct($data);
}
public function someExampleMethod() {
    /*
    some logic of method
    */
    //accessing to logger instance and calling log method
    $this->_logger->log('some text or variable');
    /// for array use this
   // $this->_logger->log(100,print_r($layout,true));
}

}

you can also print it like that to see result

print_r($layout);exit;
1

You can print any object using debug function. Review below link

https://github.com/magento/magento2/blob/2.3-develop/lib/internal/Magento/Framework/DataObject.php#L461-L489

for print layout object you can use

$layout->debug()

0

Hope, it will be helpful.

print_r($layout);
1
  • 1
    If you try print_r wirh magento objects, you run out of memory Commented Aug 13, 2021 at 8:51
0

var_dump() is a good way too, you could use <pre> to format the output.

echo "<pre>";
var_dump($layout);
echo "</pre>";
exit;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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