0

I'm reading the "Magento 2 Development cookbook" and i don't know why the creation of the controller fails. I have:

app/code/Packt/HelloWorld/etc/frontend/routes.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <router id="standard">
        <route id="helloworld" frontName="helloworld">
            <module name="Packt_HelloWorld" />
        </route>
    </router>
</config>

app/code/Packt/HelloWorld/Controller/Index/Index.php

<?php

namespace Packt\HelloWorld\Controller\Index;

class Index extends \Magento\Framework\App\Action\Action
{
    /**
    * Index action
    *
    * @return $this
    */

    public function execute ()
    {

    }
}

I clean the cache, which is the only thing needed according to the book. And, when I go to the browser /magento2/helloworld

1 exception(s):
Exception #0 (Exception): Recoverable Error: Argument 1 passed to Magento\Framework\App\Action\Action::__construct() must be an instance of Magento\Framework\App\Action\Context, instance of Magento\Framework\ObjectManager\ObjectManager given, called in /var/www/html/magento2/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php on line 93 and defined in /var/www/html/magento2/vendor/magento/framework/App/Action/Action.php on line 67

Exception #0 (Exception): Recoverable Error: Argument 1 passed to Magento\Framework\App\Action\Action::__construct() must be an instance of Magento\Framework\App\Action\Context, instance of Magento\Framework\ObjectManager\ObjectManager given, called in /var/www/html/magento2/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php on line 93 and defined in /var/www/html/magento2/vendor/magento/framework/App/Action/Action.php on line 67
#0 /var/www/html/magento2/vendor/magento/framework/App/Action/Action.php(67): Magento\Framework\App\ErrorHandler->handler(4096, 'Argument 1 pass...', '/var/www/html/m...', 67, Array)
#1 /var/www/html/magento2/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php(93): Magento\Framework\App\Action\Action->__construct(Object(Magento\Framework\ObjectManager\ObjectManager))
#2 /var/www/html/magento2/vendor/magento/framework/ObjectManager/Factory/Compiled.php(88): Magento\Framework\ObjectManager\Factory\AbstractFactory->createObject('Packt\\HelloWorl...', Array)
#3 /var/www/html/magento2/vendor/magento/framework/ObjectManager/ObjectManager.php(57): Magento\Framework\ObjectManager\Factory\Compiled->create('Packt\\HelloWorl...', Array)
#4 /var/www/html/magento2/vendor/magento/framework/App/ActionFactory.php(40): Magento\Framework\ObjectManager\ObjectManager->create('Packt\\HelloWorl...')
#5 /var/www/html/magento2/vendor/magento/framework/App/Router/Base.php(300): Magento\Framework\App\ActionFactory->create('Packt\\HelloWorl...')
#6 /var/www/html/magento2/vendor/magento/framework/App/Router/Base.php(161): Magento\Framework\App\Router\Base->matchAction(Object(Magento\Framework\App\Request\Http), Array)
#7 /var/www/html/magento2/vendor/magento/framework/App/FrontController.php(50): Magento\Framework\App\Router\Base->match(Object(Magento\Framework\App\Request\Http))
#8 /var/www/html/magento2/vendor/magento/framework/Interception/Interceptor.php(74): Magento\Framework\App\FrontController->dispatch(Object(Magento\Framework\App\Request\Http))
#9 /var/www/html/magento2/vendor/magento/framework/Interception/Chain/Chain.php(70): Magento\Framework\App\FrontController\Interceptor->___callParent('dispatch', Array)
#10 /var/www/html/magento2/vendor/magento/framework/Interception/Chain/Chain.php(63): Magento\Framework\Interception\Chain\Chain->invokeNext('Magento\\Framewo...', 'dispatch', Object(Magento\Framework\App\FrontController\Interceptor), Array, 'requestPreproce...')
#11 /var/www/html/magento2/vendor/magento/module-store/App/FrontController/Plugin/RequestPreprocessor.php(94): Magento\Framework\Interception\Chain\Chain->Magento\Framework\Interception\Chain\{closure}(Object(Magento\Framework\App\Request\Http))
#12 /var/www/html/magento2/vendor/magento/framework/Interception/Chain/Chain.php(67): Magento\Store\App\FrontController\Plugin\RequestPreprocessor->aroundDispatch(Object(Magento\Framework\App\FrontController\Interceptor), Object(Closure), Object(Magento\Framework\App\Request\Http))
#13 /var/www/html/magento2/vendor/magento/framework/Interception/Chain/Chain.php(63): Magento\Framework\Interception\Chain\Chain->invokeNext('Magento\\Framewo...', 'dispatch', Object(Magento\Framework\App\FrontController\Interceptor), Array, 'install')
#14 /var/www/html/magento2/vendor/magento/framework/Module/Plugin/DbStatusValidator.php(69): Magento\Framework\Interception\Chain\Chain->Magento\Framework\Interception\Chain\{closure}(Object(Magento\Framework\App\Request\Http))
#15 /var/www/html/magento2/vendor/magento/framework/Interception/Chain/Chain.php(67): Magento\Framework\Module\Plugin\DbStatusValidator->aroundDispatch(Object(Magento\Framework\App\FrontController\Interceptor), Object(Closure), Object(Magento\Framework\App\Request\Http))
#16 /var/www/html/magento2/vendor/magento/framework/Interception/Chain/Chain.php(63): Magento\Framework\Interception\Chain\Chain->invokeNext('Magento\\Framewo...', 'dispatch', Object(Magento\Framework\App\FrontController\Interceptor), Array, 'front-controlle...')
#17 /var/www/html/magento2/vendor/magento/module-page-cache/Model/App/FrontController/BuiltinPlugin.php(68): Magento\Framework\Interception\Chain\Chain->Magento\Framework\Interception\Chain\{closure}(Object(Magento\Framework\App\Request\Http))
#18 /var/www/html/magento2/vendor/magento/framework/Interception/Chain/Chain.php(67): Magento\PageCache\Model\App\FrontController\BuiltinPlugin->aroundDispatch(Object(Magento\Framework\App\FrontController\Interceptor), Object(Closure), Object(Magento\Framework\App\Request\Http))
#19 /var/www/html/magento2/vendor/magento/framework/Interception/Interceptor.php(138): Magento\Framework\Interception\Chain\Chain->invokeNext('Magento\\Framewo...', 'dispatch', Object(Magento\Framework\App\FrontController\Interceptor), Array, 'front-controlle...')
#20 /var/www/html/magento2/vendor/magento/module-page-cache/Model/App/FrontController/VarnishPlugin.php(55): Magento\Framework\App\FrontController\Interceptor->Magento\Framework\Interception\{closure}(Object(Magento\Framework\App\Request\Http))
#21 /var/www/html/magento2/vendor/magento/framework/Interception/Interceptor.php(142): Magento\PageCache\Model\App\FrontController\VarnishPlugin->aroundDispatch(Object(Magento\Framework\App\FrontController\Interceptor), Object(Closure), Object(Magento\Framework\App\Request\Http))
#22 /var/www/html/magento2/var/generation/Magento/Framework/App/FrontController/Interceptor.php(26): Magento\Framework\App\FrontController\Interceptor->___callPlugins('dispatch', Array, Array)
#23 /var/www/html/magento2/vendor/magento/framework/App/Http.php(135): Magento\Framework\App\FrontController\Interceptor->dispatch(Object(Magento\Framework\App\Request\Http))
#24 /var/www/html/magento2/vendor/magento/framework/App/Bootstrap.php(258): Magento\Framework\App\Http->launch()
#25 /var/www/html/magento2/index.php(39): Magento\Framework\App\Bootstrap->run(Object(Magento\Framework\App\Http))
#26 {main}
2

Please try this.

<?php
namespace Packt\HelloWorld\Controller\Index;

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

    /**
     * @var \Magento\Framework\View\Result\PageFactory
     */
    protected $resultPageFactory;

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

    /**
     * Loads page content
     *
     * @return \Magento\Framework\View\Result\Page
     */
    public function execute()
    {
        echo 'called';exit;
        $resultPage = $this->resultPageFactory->create();
        $resultPage->getConfig()->getTitle()->set(__(''));
        return $resultPage;
    }

}
1

Recoverable Error: Argument passed ... must be an instance of ....

This error usually relates to the arguments of constructor. Each time we call our Controller, the Interceptor which derives from our Controller will be called from var/generation. If we change the constructor of our controller, the Interceptor still keeps the old arguments. They are not compatible. So, our error will be thrown.

We should delete this folder, the new Interceptor Controller file will be generated automatically again with the new constructor.

For example Interceptor:

var/generation/Magento/Catalog/Controller/Category/View/Interceptor.php

class Interceptor extends \Magento\Catalog\Controller\Category\View implements \Magento\Framework\Interception\InterceptorInterface 

Read more here: Magento 2 - What is file Interceptor.php?

-1

You need put constructer in your controller like this,

<?php

namespace Packt\HelloWorld\Controller\Index;

class Index extends \Magento\Framework\App\Action\Action
{
    public function __construct(\Magento\Framework\App\Action\Context $context)
    {
        parent::__construct($context);
    }

    public function execute ()
    {
        echo "This is my page";
    }
}
  • 2
    I'm sure you don't need that. If the constructor is missing from a class, the parent class constructor will be called anyway. – Marius Jan 5 '17 at 12:45
-1

Delete var/generation and pub/static than

bin/magento setup:static-content:deploy

it works for me

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.