Magento 2 – Undefined Property _pageResult on Admin Page

adminadminhtmlmagento2

I created route and admin menu item linked to this route, but I am getting the error message in the browser when I try to access it:

1 exception(s):
Exception #0 (Exception): Notice: Undefined property: Namespace\Mymodule\Controller\Adminhtml\Griglia\Index\Interceptor::$_resultPage in /Applications/MAMP/htdocs/magento2/app/code/Namespace/Mymodule/Controller/Adminhtml/Griglia/Index.php on line 34 ...... ............(MORE ERROR TEXT IS HERE) ....

I can see the content of the page on this route, if I cut the processing by outputting something in execute method and return;, but I want the template to load too, using the layout and template for this admin page.

Code in Controller/Adminhtmk/Griglia/Index.php is:

<?php

namespace Namespace\Mymodule\Controller\Adminhtml\Griglia;

class Index extends \Magento\Backend\App\Action
{
    protected $resultPageFactory = false;
    public function __construct(
        \Magento\Backend\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory
    ) {
        parent::__construct($context);
        $this->resultPageFactory = $resultPageFactory;
    }

    public function execute()
    {

        //Call page factory to render layout and page content
        $this->_setPageData();
        return $this->getResultPage();
    }

    /*
     * Check permission via ACL resource
     */
    protected function _isAllowed()
    {
        return $this->_authorization->isAllowed('Namespace_Mymodule::griglia');
    }

    public function getResultPage()
    {
        if (is_null($this->_resultPage)) {
            $this->_resultPage = $this->_resultPageFactory->create();
        }
        return $this->_resultPage;
    }

    protected function _setPageData()
    {
        $resultPage = $this->getResultPage();
        $resultPage->setActiveMenu('Namespace_Mymodule::griglia');
        $resultPage->getConfig()->getTitle()->prepend((__('Posts')));


        return $this;
    }


}

Best Answer

Add this line above the constructor:

protected $_resultPage;

and in the method getResultPage replace this

$this->_resultPage = $this->_resultPageFactory->create();

with this

$this->_resultPage = $this->resultPageFactory->create();
Related Topic