5

I'm trying to create a block I can call in my override of 'root.phtml'.

In the root file, I added some extra classes to the body depending on the url, so I've created a module which I call on this page.

I've overridden root.phtml in my theme folder, and I've created a seperate module to call in this phtml file. This is just to get the current URL (so if you have a better suggestion, please come forward) however when I try to load the application it comes up with

    Missing required argument $data of Karl\OverrideView\Block\OverrideView

The file being refered to is;

namespace Karl\OverrideView\Block;

class OverrideView
{

public $assetRepository;
protected $_storeManager;

public function __construct(

    \Magento\Framework\View\Element\Template\Context $context,
    array $data = [],
    \Magento\Store\Model\StoreManagerInterface $storeManager
){
    $this->_storeManager = $storeManager;
    return parent::construct($context, $data);
}

public function getFullUrl(){
    return $this->_storeManager->getStore()->getCurrentUrl(false);
}
}
12

In your constructor you are calling parent::construct(), but you are not extending any class. You must extend the class to which you are override or to the class \Magento\Framework\View\Element\Template and there is a typo in constructor name (two underscores are missing), the proper name is __construct

Also, write your constructor this way:

public function __construct(

    \Magento\Framework\View\Element\Template\Context $context,
    \Magento\Store\Model\StoreManagerInterface $storeManager,
    array $data = []
){
    $this->_storeManager = $storeManager;
    return parent::__construct($context, $data);
}
  • Realised I'd not given this answer any credit, Thanks! – ol'bob dole Oct 9 '17 at 13:24
0

I was having the same error, all my __constructs and Extending's are correct, if you are scratching your head wondering... why when I do:

php bin/magento setup:upgrade

it breaks everything and get this error, but then when I do:

php bin/magento setup:di:compile

the same code works fine afterwards, but then would break again if you do another:

php bin/magento setup:upgrade

??

Then most likely is that you have got your array $DATA in the middle of your Construct definition, this would cause this issue and the solution is to move this right to the end of your __constrct definition, see example below:

Example, this will NOT work:

public function __construct (
      \Magento\Framework\Model\Context $ModelContext
    , array $Data = []
    , \Magento\Framework\Registry $Registry (

But this will work:

public function __construct (
      \Magento\Framework\Model\Context $ModelContext
    , \Magento\Framework\Registry $Registry ( 
    , array $Data = []

I hope this helps.

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.