3

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 ?

asked Oct 8, 2017 at 6:45
1
  • did you get the solution??? actually I'm facing the same issue Commented Sep 20, 2019 at 9:47

2 Answers 2

1

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;

answered Oct 8, 2017 at 7:31
1
  • How come swapping two lines work - ` $this->resultPageFactory = $resultPageFactory; parent::__construct( $context ); ?` Commented Oct 8, 2017 at 7:35
1

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

answered Oct 8, 2017 at 7:48
3
  • It did not worked. Commented Oct 8, 2017 at 8:23
  • Still there is error? Commented Oct 8, 2017 at 8:33
  • 1
    No, 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. Commented Oct 8, 2017 at 8:36

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.