1

I'm using Magento 2 CE Version 2.1.0

I'm following http://www.mage-world.com/blog/how-to-create-a-simple-module-in-magento-2-0.html

I have followed steps when i run it gives below error.

{folder path}\magento2\app\code\Custom\Module\Controller\Index\Index.php

namespace Custom\Module\Controller\Index;

use Magento\Framework\Controller\Result\ForwardFactory;
use Magento\Framework\App\Action\Context;

class Index extends \Magento\Framework\App\Action\Action {

    /**
     * @var \Magento\Framework\Controller\Result\ForwardFactory
     */
    protected $resultForwardFactory;

    /**
     * @param \Magento\Framework\App\Action\Context $context
     * @param \Magento\Framework\Controller\Result\ForwardFactory $resultForwardFactory
     */
    public function __construct(
    \Magento\Framework\App\Action\Context $context, \Magento\Framework\Controller\Result\ForwardFactory $resultForwardFactory
    ) {
        $this->resultForwardFactory = $resultForwardFactory;
        parent::__construct($context);
    }

    public function execute() {
        $resultPageFactory = $this->resultPageFactory->create();

        // Add page title
        $resultPageFactory->getConfig()->getTitle()->set(__('Sample Module'));

        // Add breadcrumb
        /** @var \Magento\Theme\Block\Html\Breadcrumbs */
        $breadcrumbs = $resultPageFactory->getLayout()->getBlock('breadcrumbs');
        $breadcrumbs->addCrumb('home', [
            'label' => __('Home'),
            'title' => __('Home'),
            'link' => $this->_url->getUrl('')
                ]
        );
        $breadcrumbs->addCrumb('custom_module', [
            'label' => __('Test'),
            'title' => __('Test')
                ]
        );
        return $resultPageFactory;
    }
}

1 exception(s): Exception #0 (Exception): Recoverable Error: Argument 2 passed to Custom\Module\Controller\Index\Index::__construct() must be an instance of Magento\Framework\Controller\Result\ForwardFactory, none given, called in {folder path}\magento2\var\generation\Custom\Module\Controller\Index\Index\Interceptor.php on line 14 and defined in {folder path}\magento2\app\code\Custom\Module\Controller\Index\Index.php on line 21

3

1 Answer 1

6

Add this Code to vendor\Module\Controller\Index/Index.php

    <?php
    namespace vendor\Module\Controller\Index;

    use Magento\Framework\View\Result\PageFactory;
    use Magento\Framework\App\Action\Context;

    class Index extends \Magento\Framework\App\Action\Action {

        /**
         * @var \Magento\Framework\Controller\Result\ForwardFactory
         */
        protected $resultPageFactory;

        /**
         * @param \Magento\Framework\App\Action\Context $context
         * @param \Magento\Framework\Controller\Result\ForwardFactory $resultForwardFactory
         */
        public function __construct(
        \Magento\Framework\App\Action\Context $context, \Magento\Framework\View\Result\PageFactory $resultPageFactory
        ) {
            $this->resultPageFactory = $resultPageFactory;
            parent::__construct($context);
        }

        public function execute() {
            $resultPageFactory = $this->resultPageFactory->create();

            // Add page title
            $resultPageFactory->getConfig()->getTitle()->set(__('Sample Module'));

            // Add breadcrumb
            /** @var \Magento\Theme\Block\Html\Breadcrumbs */
            $breadcrumbs = $resultPageFactory->getLayout()->getBlock('breadcrumbs');
            $breadcrumbs->addCrumb('home', [
                'label' => __('Home'),
                'title' => __('Home'),
                'link' => $this->_url->getUrl('')
                    ]
            );
            $breadcrumbs->addCrumb('custom_module', [
                'label' => __('Test'),
                'title' => __('Test')
                    ]
            );
            return $resultPageFactory;
        }
    }

After Run

      bin/magento setup:upgrade

remove var/*

       rm -rf var/*

Now, check This is Works for me.

1
  • Got the Issue Need to use: \Magento\Framework\View\Result\PageFactory $resultPageFactory. Thanks it works fine :)
    – Jackson
    Aug 18, 2016 at 6:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.