2

I have a custom controller which I'm trying to get a block rendered.

Company/Module/Controller/Index/Index.php

<?php

namespace Company\Module\Controller\Index;

use Magento\Framework\App\Action\Action;
use Magento\Framework\Controller\ResultFactory;


class Index extends Action
{

    public function execute()
    {
        return $this->resultFactory->create(ResultFactory::TYPE_PAGE);
    }
}

Company/Module/Block/Hello.php

<?php

namespace Company\Module\Block;

use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;

class Hello extends Template
{

}

Company/Module/etc/frontend/routes.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <router id="standard">
        <route id="testing_hello" frontName="testing">
            <module name="Company_Module"/>
        </route>
    </router>
</config>

Company/Module/view/frontend/layout/testing_index_index.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="content">
            <block name="custom.name" class="Company\Module\Block\Hello" template="testing.phtml"/>
        </referenceContainer>
    </body>
</page>

Company/Module/view/frontend/templates/testing.phtml

<h2>Hello World</h2>

For some reason I don't see <h2>Hello World</h2> I just see blank / empty content area but the rest of the page is loaded correctly.

I'll really appreciate any help on this.

2 Answers 2

2

For this issue

  • either you should changes layout name
  • Or Changes the route name

Explanation:

whenever a page a render that times a handler is will automatically.

That handler is {route_id}_{controllerName}_{ActioName}

And for every page, magento load a layout file automatically that is

{route_id}_{controllerName}_{ActioName}.xml

Example,Customer account creation page customer/account/create load customer_account_creata.xml because of its has a handler customer_account_creata.

As you have checked vendor/magento/module-customer/module-customer/etc/frontend/routes.xml.

And you will find route id is customer .<route id="customer" frontName="customer">.

SO, either you will changes

route id="testing_hello"

to

route id="testing"

Or change layout files name

testing_index_index.xml

to

testing_hello_index_index.xml

2
  • Thanks, now I'm getting an another error; "Invalid template file" though.
    – Revenant
    Commented Oct 17, 2017 at 17:48
  • 1
    Please changes template="testing.phtml" to template="Company/Module::testing.phtml" and check .but must insure that you should flush cache properly
    – Amit Bera
    Commented Oct 17, 2017 at 17:55
2

Try replace line in Company/Module/etc/frontend/routes.xml:

<route id="testing_hello" frontName="testing">

to:

<route id="testing" frontName="testing">

Don't forget clean cache after modifications. Your controller will be available at #your_domain#/testing/index/index

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.