convertm1m2 icon indicating copy to clipboard operation
convertm1m2 copied to clipboard

Controllers Constructors Don't include Parent Calls

Open astorm opened this issue 7 years ago • 5 comments

When I've used this script, it seems to generate abstract controllers that look like this

public function __construct(ScopeConfigInterface $configScopeConfigInterface)
{
    $this->_configScopeConfigInterface = $configScopeConfigInterface;
}

That is, they don't include calls to the parent controller, and don't inject a Context object. This means the controllers will never work out of the box, and require the user to dig back through the controller hierarchy chain to pull out the needed arguments themselves.

Ideally the script should look at the parent controller. If it can't find the parent controller then assuming a stock admin or frontend action controller seems like a better approach.

astorm avatar Jun 12 '17 16:06 astorm

Actually the Abstract controller class should take the __construct from the original class. Works for me :) Could you please paste an isolated example of the controller that doesn't get converted properly?

unirgy avatar Jun 12 '17 19:06 unirgy

Also, if there's no __construct in original class, during DI conversion, it would attempt find the parent with constructor and apply proper arguments and add parent::__construct(). See line 2239

unirgy avatar Jun 12 '17 19:06 unirgy

This file (a simplified version of a real file)

<?php
require_once 'Mage/Contacts/controllers/IndexController.php';

class ABC_Contacts_TestController extends Mage_Contacts_IndexController
{
    public function preDispatch()
    {
        parent::preDispatch();
    }

    public function indexAction()
    {
        if( !Mage::getStoreConfigFlag(self::XML_PATH_ENABLED) ) {
            $this->norouteAction();
        }    
    }

    public function postAction()
    {
    }
}

Converts into an abstract controller, an index controller, and a post controller. The Index controller looks like this

<?php

namespace ABC\Contacts\Controller\Test;

use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Store\Model\ScopeInterface;
use XML\PATH\ENABLED;

class Index extends AbstractTest
{
    /**
     * @var ScopeConfigInterface
     */
    protected $_configScopeConfigInterface;

    public function __construct(ScopeConfigInterface $configScopeConfigInterface)
    {
        $this->_configScopeConfigInterface = $configScopeConfigInterface;

    }

    public function execute()
    {
        if( !$this->_configScopeConfigInterface->isSetFlag(self::ENABLED, ScopeInterface::SCOPE_STORE) ) {
            $this->norouteAction();
        }    
    }
}

Notice the constructor contains no context object, and no call back to the parent.

astorm avatar Jun 12 '17 22:06 astorm

And that reminded me why I've added safeguard against all caps classes...

unirgy avatar Jun 12 '17 22:06 unirgy

I know that feel.

astorm avatar Jun 12 '17 22:06 astorm