0
 namespace My\Module\Controller\Index;

 use Magento\Framework\App\Action\Context;

class Index extends \Magento\Framework\App\Action\Action
{
protected $_resultPageFactory;

public function __construct(Context $context, \Magento\Framework\View\Result\PageFactory $resultPageFactory)
{
    $this->_resultPageFactory = $resultPageFactory;
    parent::__construct($context);
}

public function execute()
{
    $resultPage = $this->_resultPageFactory->create();
    $block = $resultPage->getLayout()->createBlock(
        '\Magento\Framework\View\Element\Template',
        'my.block',
        array('template' => 'My_Module/test.phtml')
    )->toHtml();


    if($resultPage->getLayout()->getBlock('content')){
        $resultPage->getLayout()->getBlock('content')
            ->append($block);
    }

    return $resultPage;
  }
}

The code above in controller returns :

Fatal error: Uncaught Error: Call to a member function append() on boolean...

Any ideas? thanks

  • What you want to do actually? – Rohan Hapani Nov 2 '18 at 12:44
  • Please share the full code of controller – Amit Bera Nov 2 '18 at 13:33
0

As per as, your share code seems only issue

$resultPage->getLayout()->getBlock('content.bottom')

Maybe content.bottom the section is not rendered this page and you have try to append another block from on non-exits section.

So, changes $resultPage->getLayout()->getBlock('content.bottom') with $resultPage->getLayout()->getBlock('content') and check what happen?

OR

add this below condition

if($resultPage->getLayout()->getBlock('content.bottom')){
    $resultPage->getLayout()->getBlock('content.bottom')
     ->append($block);
} 

    return $resultPage

Instead of

$resultPage->getLayout()->getBlock('content.bottom')
     ->append($block);
    return $resultPage;
  • ♦ That isn't not falling into the if doesn't work – Juliano Vargas Nov 2 '18 at 14:43

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.