2

I'm trying to create a custom block in the theme I created for Magento 2.1.8.

When I add the element to the default.xml layout, I got the following exception:

Exception #0 (Magento\Framework\Exception\LocalizedException): Invalid block type: frontend\Custom\Simple\Block\Services
Exception #1 (ReflectionException): Class frontend\Custom\Simple\Block\Services does not exist

In the Custom/Simple/Block/Services.php I have the following code:

<?php

namespace Custom\Simple\Block;
use Magento\Framework\View\Element\Template;

/**
* Services block
*/
class Services extends Template {
    public function getTitle() {
        return "Services";
    }
}

In the default.xml I've added this code:

<referenceContainer name="content">
    <block class="frontend\Custom\Simple\Block\Services" name="services.test" template="Custom_simple::test.phtml">
        <arguments>
            <argument name="something" xsi:type="string">Value</argument>
        </arguments>
    </block>
</referenceContainer>

I've tried to change the class to: Custom\Simple\Block\Services and I got the same error.

Thanks for your help!

Edit:

In the registration.php I have the following code:

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::THEME,
'frontend/Customdac/Simple',
__DIR__
);
0

First thing is that all this code should be located at app\code\Custom\Simple because you are creating a module not a theme.

And the other thing is you are giving wrong block & template location in the default.xml file give it as below & this file should be located at app\code\Custom\Simple\view\frontend\layout\default.xml

<referenceContainer name="content">
        <block class="Custom\Simple\Block\Services" name="services.test" template="Custom_Simple::test.phtml">
            <arguments>
                <argument name="something" xsi:type="string">Value</argument>
            </arguments>
        </block>
</referenceContainer>

and for the registration.php file there should be this code for the registration of the module & this file should be located at app\code\Custom\Simple\registration.php

<?php

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Custom_Simple',
    __DIR__
);
  • to insert your custom blocks into your theme you need to create modules it will not work from theme – Abhishek Tripathi Oct 2 '17 at 12:34
  • Thank you! I came to the same conclusion after a couple of working hours :) – qvasz Oct 2 '17 at 21:05
  • if my answer was help full to you don't forget to vote & accept it so it could be helpful to others also facing the same problem – Abhishek Tripathi Oct 3 '17 at 5:44
0

Use

class="Custom\Simple\Block\Services"

Instead of

class="frontend\Custom\Simple\Block\Services"

  • Thanks but it still not working. :( In the registration.php, should I write "frontend\Custom\Simple" or without frontend? – qvasz Oct 2 '17 at 11:53
  • without frontend and it should be located at app\code\Custom\Simple – Amit Bera Oct 2 '17 at 11:55
  • If I want to add a block to my theme, shouldn't it be located at app\design\frontend\Custom\Simple? I have to write a module for it instead? – qvasz Oct 2 '17 at 12:00
  • Block class never located at app\design\frontend\Custom\Simple,it template files can be located at app\design\frontend\{VendorName}\{ThemeName}\templates\Custom_Simple\test.phtml – Amit Bera Oct 2 '17 at 12:04
  • 1
    mageplaza.com/magento-2-module-development/… checkout this link – Amit Bera Oct 2 '17 at 12:09

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.