Magento 2 – Custom Frontend Route Mistaken for Admin Area

magento2

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.

Best Answer

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

Related Topic