0

I am creating a custom route as an example. Here is the code;

In 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="foo" frontName="foo">
            <module name="[Vendor]_[ModuleName]" />
        </route>
    </router>
</config>

In Controller/Index/Index.php

<?php

namespace [Vendor]\[ModuleName]\Controller\Index;

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

class Index extends Action\Action
{
    public function execute()
    {
        $page = $this->resultFactory->create(ResultFactory::TYPE_PAGE);
        return $page;
    }
}

Now if I go to /foo I get a blank page, when I am expecting to at least see the site outline with an empty content area.

If I look at the source I see html but nothing in the body. The interesting thing are the classes on the body;

class="foo-index-index page-layout-admin-1column"

Now I expect to see foo-index-index but why would page-layout-admin-1column exist. I do not have a layout file for this handle.

2
  • Layout appears when you mention it in layout.xml file for your corresponding controller. Can you please post your layout file code of controller_index_index.xml ? Mar 9, 2016 at 6:54
  • There is no layout file associated with the controller.
    – Smartie
    Mar 9, 2016 at 9:58

1 Answer 1

0

Add following file at /VendorName/ModuleName/view/frontend/layout/controller_index_index.xml

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="3columns" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"> 
    <body>
        <referenceContainer name="content">
        </referenceContainer>
    </body>
</page>

Notice layout="3columns" in above code. This will help display the site outline with an empty content area. More details here: http://devdocs.magento.com/guides/v2.0/frontend-dev-guide/layouts/xml-manage.html#layout_markup_columns

In case you want to set default layout dynamically for your custom module, you might want to look at this post: Magento 2 - Set the page layout dynamically based on admin configuration

Hope this helps !!

1
  • Creating the XML does work as you suggest. I wasn't aware that it was a requirement but after doing some digging it looks like if you do not specify a page layout it defaults to admin-1column. Doesn't seem like a sensible default to me but that is for another day.
    – Smartie
    Mar 10, 2016 at 15:02

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.