2

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 ?

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

  • It did not worked. – Slimshadddyyy Oct 8 '17 at 8:23
  • Still there is error? – Prashant Valanda Oct 8 '17 at 8:33
  • 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. – Slimshadddyyy Oct 8 '17 at 8:36
0

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 Oct 8 '17 at 7:35

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.