0
votes

Here is my controller action class

<?php
namespace Felix\HelloMod\Controller\Files;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;

class ProductFiles extends Action
{
    protected $_pagefactory;
    public function _construct(Context $context, PageFactory $pageFactory){
        $this->_pagefactory = $pageFactory;
        return parent::_construct($context);
    }
    public function execute(){
        return $this->_pagefactory->create();
    }
}

I am having the following error:

Fatal error: Uncaught Error: Call to a member function create() on null in C:\xampp\htdocs\Mage2\app\code\Felix\HelloMod\Controller\Files\ProductFiles.php:15 Stack trace: #0 C:\xampp\htdocs\Mage2\generated\code\Felix\HelloMod\Controller\Files\ProductFiles\Interceptor.php(37): Felix\HelloMod\Controller\Files\ProductFiles->execute() #1 C:\xampp\htdocs\Mage2\vendor\magento\framework\App\Action\Action.php(107): Felix\HelloMod\Controller\Files\ProductFiles\Interceptor->execute() #2 C:\xampp\htdocs\Mage2\vendor\magento\framework\Interception\Interceptor.php(58): Magento\Framework\App\Action\Action->dispatch(Object(Magento\Framework\App\Request\Http)) #3 C:\xampp\htdocs\Mage2\vendor\magento\framework\Interception\Interceptor.php(138): Felix\HelloMod\Controller\Files\ProductFiles\Interceptor->___callParent('dispatch', Array) #4 C:\xampp\htdocs\Mage2\vendor\magento\framework\Interception\Interceptor.php(153): Felix\HelloMod\Controller\Files\ProductFiles\Interceptor->Magento\Framework\Interception{closure}(Object(Magento\Framework in C:\xampp\htdocs\Mage2\app\code\Felix\HelloMod\Controller\Files\ProductFiles.php on line 15

1
$pageFactory is undefined. Probably because it wasn't correctly passed to the class.T K
I think i did define it at the top before the class definition.How do you suggest I define it.Iron Brew
Where do you use this class? There you'll need to check if you pass the right parameter to the ProductFiles ClassT K
its the controller action class and its calling the create method so i don't know why that error.Iron Brew
Where does $pageFactory come from? It is undefined.T K

1 Answers

2
votes

I missed the double underscore to the constructor method and run php bin/magento setup:di:compile and it worked.

<?php
namespace Felix\HelloMod\Controller\Files;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;

class ProductFiles extends Action
{
    protected $_pagefactory;
    public function __construct(Context $context, PageFactory $pageFactory){
        $this->_pagefactory = $pageFactory;
        return parent::__construct($context);
    }
    public function execute(){
        return $this->_pagefactory->create();
    }
}