質問

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!

役に立ちましたか?

解決

 <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.

ライセンス: CC-BY-SA帰属
所属していません magento.stackexchange
scroll top