1

I get this error in a block that I am developing to get current category. any ideas?

class CurrentCategory extends \Magento\Framework\View\Element\Template
{

protected $_registry;

public function __construct(
    \Magento\Framework\Registry $registry
)
{
    $this->_registry = $registry;
}

public function getCurrentCategory()
{
    return $this->_registry->registry('current_category');
}

PHP Fatal error: Uncaught Error: Call to a member function dispatch() on null in /vendor/magento/framework/View/Element/AbstractBlock.php:652

  • If you have found an answer please use arrows/checkmark to upvote/accept an answer. Thank you. – sv3n Dec 10 '18 at 10:32
0

Try below code

class CurrentCategory extends \Magento\Framework\View\Element\Template
{

protected $_registry;

public function __construct(
    \Magento\Framework\View\Element\Template\Context $context,
    \Magento\Framework\Registry $registry
)
{
    parent::__construct($context);
    $this->_registry = $registry;
}

public function getCurrentCategory()
{
    return $this->_registry->registry('current_category');
}

run upgrade, compile and deploy command and clear cache

0

You can simply inherit (or use, if you don't want to add functionality) from \Magento\Catalog\Block\Category\View instead of \Magento\Framework\View\Element\Template to get access to the getCurrentCategory() method.

0

You constructor has to match the parent one ... please try this:

public function __construct
(
    \Magento\Framework\View\Element\Template\Context $context,
    \Magento\Framework\Registry $registry
    array $data = []
) {
    parent::__construct($context, $data);
    $this->_registry = $registry;
}

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.