0

I am using Magento 2.2.6. In my custom module I add catalog_category_view.xml add a block

<?xml version="1.0" ?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="sidebar.main">
            <block class="Vendor\Module\Block\CategoryListing"  name="sidebar.vendor.category" before="catalog.navigation.renderer" cacheable="false" template="Vendor_Module::category_list.phtml"/>
        </referenceContainer>
    </body>
</page>

and class is as

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

    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context
    )
    {
        parent::__construct($context);
    }

    public function welcome()
    {
        .....
    }
}

but page through error.

Fatal error: Uncaught Error: Call to a member function get() on null in /home/user/public_html/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php:144 Stack trace: #0 

But without class in layout xml as

<block name="sidebar.vendor.category" before="catalog.navigation.renderer" cacheable="false" template="Vendor_Module::category_list.phtml"/>

Works fine i.e. load category_list.phtml with its content. But my target is using the block with class.

Where is the problem? Thanks!

  • can you please add your error here ? – Aasim Goriya Feb 4 '19 at 8:20
  • 1
    I think you have written wrong. like class shows vendor\module and template file shows Vendor_Core. You can change the class and template path properly. What is the module name you are using? – Sukumar Gorai Feb 4 '19 at 8:21
  • I updated my question. – Iftakharul Alam Feb 4 '19 at 8:41
2
 <body>
    <referenceContainer name="sidebar.main">
            <block class="Vendor\Module\Block\CategoryListing"  name="sidebar.vendor.category" before="catalog.navigation.renderer" cacheable="false" template="Vendor_Module::category_list.phtml"/>
        </referenceContainer>
    </body>
  • Be sure your file CategoryListing.php located in Vendor/Module/Block folder
  • Be sure category_list.phtml file located in Vendor/Module/view/frontend/templates folder
  • Be sure your file catalog_category_view.xml located in Vendor/Module/view/frontend/layout folder

May be you have made some spell-mistakes and capitalize like class="Vendor\Module\Block\CategoryListing"

EDIT

Your constructor does not match the parent class constructor after seeing your error.

To fix that you need to update your constructor:

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

Don't forget to flush the var/cache and var/generation after your changes.

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.