Controller File
<?php
namespace Magento2\HelloWorld\Controller\Index;
use \Magento\Framework\App\Action\Action;
use \Magento\Framework\View\Result\PageFactory;
use \Magento\Framework\View\Result\Page;
use \Magento\Framework\App\Action\Context;
use \Magento\Framework\Exception\LocalizedException;
class Index extends Action
{
 /**
 * @var PageFactory
 */
 protected $resultPageFactory;
 /**
 * @param Context $context
 * @param PageFactory $resultPageFactory
 *
 * @codeCoverageIgnore
 * @SuppressWarnings(PHPMD.ExcessiveParameterList)
 */
 public function __construct(
 Context $context,
 PageFactory $resultPageFactory
 ) {
 parent::__construct(
 $context
 );
 $this->resultPageFactory = $resultPageFactory;
 }
 public function execute()
 {
 $resultPage = $this->resultPageFactory->create();
 return $resultPage;
 }
}
It throws below exception while trying to run controller, while I have already mentioned use \Magento\Framework\View\Result\PageFactory;
Fatal error: Uncaught TypeError: Argument 2 passed to Magento2\HelloWorld\Controller\Index\Index::__construct() must be an instance of Magento\Framework\View\Result\PageFactory
If I simply use below, it works
<?php
namespace Magento2\HelloWorld\Controller\Index;
use \Magento\Framework\App\Action\Action;
use \Magento\Framework\View\Result\PageFactory;
use \Magento\Framework\View\Result\Page;
use \Magento\Framework\App\Action\Context;
use \Magento\Framework\Exception\LocalizedException;
class Index extends Action
{
 public function execute()
 {
 $this->_view->loadLayout();
 $this->_view->getLayout()->initMessages();
 $this->_view->renderLayout();
 }
}
How can I resolve the __construct() error ?
- 
 did you get the solution??? actually I'm facing the same issueAsad Khan– Asad Khan2019年09月20日 09:47:29 +00:00Commented Sep 20, 2019 at 9:47
2 Answers 2
Change __construct function to
public function __construct(
 Context $context,
 PageFactory $resultPageFactory
) {
 $this->resultPageFactory = $resultPageFactory;
 parent::__construct(
 $context
 );
}
and Change
class Index extends \Magento\Framework\App\Action\Action
from
class Index extends Action
Also remove
use \Magento\Framework\App\Action\Action;
- 
 How come swapping two lines work - ` $this->resultPageFactory = $resultPageFactory; parent::__construct( $context ); ?`Slimshadddyyy– Slimshadddyyy2017年10月08日 07:35:40 +00:00Commented Oct 8, 2017 at 7:35
Your code is correct there is no any issue.
Follow below step
1) remove cache: php bin/magento cache:flush
2) remove var/generation : rm -rf var/generation
- 
 It did not worked.Slimshadddyyy– Slimshadddyyy2017年10月08日 08:23:35 +00:00Commented Oct 8, 2017 at 8:23
- 
 Still there is error?Prashant Valanda– Prashant Valanda2017年10月08日 08:33:47 +00:00Commented Oct 8, 2017 at 8:33
- 
 1No, error is resolved by swapping these two lines in construct function -$this->resultPageFactory = $resultPageFactory; parent::__construct( $context );. Although I tried clearing cache and var/generation, but it did not helped.Slimshadddyyy– Slimshadddyyy2017年10月08日 08:36:52 +00:00Commented Oct 8, 2017 at 8:36