1

I create a file custom block php.

Directory is: app\design\frontend\Vendor\default\Magento_Theme\Block\CustomBlock.php

CustomBlock.php:

class CustomBlock extends \Magento\Framework\View\Element\Template
{
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Store\Model\Information $storeInfo,
        array $data = []
    )
    {
        $this->_storeInfo = $storeInfo;
        parent::__construct($context, $data);
    }
}

Define block xml:

<block class="Magento\Theme\Block\CustomBlock" name="...">
                <argument name="template" xsi:type="string">Magento_Theme::html/footer/footer.phtml</argument>
            </block>

But I get an exception:

Exception #0 (Magento\Framework\Exception\LocalizedException): Invalid block type: Magento\Theme\Block\CustomBlock
Exception #1 (ReflectionException): Class Magento\Theme\Block\CustomBlockdoes not exist

Please help me!

Ghulam.M
  • 963
  • 8
  • 24

3 Answers3

0

you are not supposed to create block in theme directory. Instead create your own module And put you custom block there.

You block path should be something like this.

app\code\Vendor\Module\Block\CustomBlock.php

EDIT: Module will be yours module not Magento_Theme.

You can get so many tutorials on the internet how to create module in magento2.

You can refer this How to create simple module in magento 2

Mohammad Mujassam
  • 2,170
  • 7
  • 26
  • 29
0

Just clear var/generation folder. And give write permissions to var again. Make sure in your customblock.php you have defined namespace. Then try to run your code it will work. For every class there is a cache generated inside generation folder for reflection ,factory, interceptor and proxy classes.

Kumar M
  • 787
  • 1
  • 4
  • 6
0

You can refer this link for creating a custom block https://www.mageplaza.com/magento-2-module-development/view-block-layout-template-magento-2.html

Dinesh Yadav
  • 6,447
  • 2
  • 23
  • 50