5

I put custom module in app/code/. Layout is not loading. On my custom route is blank white page. Debug showing that handles =null. This is code /app/code/Namespace/ModuleName/view/frontend/layout/test_index_index.xml

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column-left" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
    <referenceContainer name="content">
        <block class="namespace\Modulename\Block\ModuleBlock" name="form" template="namespace_ModuleName::form.phtml" />
    </referenceContainer>
       </body>

Namespace/Modulename/Controller/Index/Index.php

 namespace Namespace\ModuleName\Controller\Index;

class Index extends \Namespace\ModuleName\Controller\Index
{
    /**
     *
     *
     */
    public function execute()
    {

        $this->_view->loadLayout();
        $this->_view->renderLayout();
    }
}

**/app/code/Namespace/ModuleName/Controller/Index.php

<?php
namespace Namespace\ModuleName\Controller;
use Magento\Framework\Exception\NotFoundException;
use Magento\Framework\App\RequestInterface;
use Magento\Store\Model\ScopeInterface;

/**
 * Contact index controller
 */
abstract class Index extends \Magento\Framework\App\Action\Action
{
    /**
     * Recipient email config path
     */
    const XML_PATH_EMAIL_RECIPIENT = 'trans_email/ident_general/email';

    /**
     * Recipient email config path
     */
    const XML_PATH_EMAIL_RECIPIENT_2 = 'contact/email/recipient_email';

    /**
     * Sender email config path
     */
    const XML_PATH_EMAIL_SENDER = 'contact/email/sender_email_identity';

    /**
     * Email template config path
     */
    const XML_EMAIL_TEMPLATE = 'custom_cats_email_template';


    /**
     * @var \Magento\Framework\Mail\Template\TransportBuilder
     */
    protected $_transportBuilder;

    /**
     * @var \Magento\Framework\Translate\Inline\StateInterface
     */
    protected $inlineTranslation;

    /**
     * @var \Magento\Framework\App\Config\ScopeConfigInterface
     */
    protected $scopeConfig;

    /**
     * @var \Magento\Store\Model\StoreManagerInterface
     */
    protected $storeManager;

    /**
     * @param \Magento\Framework\App\Action\Context $context
     * @param \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder
     * @param \Magento\Framework\Translate\Inline\StateInterface $inlineTranslation
     * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
     * @param \Magento\Store\Model\StoreManagerInterface $storeManager
     */
    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder,
        \Magento\Framework\Translate\Inline\StateInterface $inlineTranslation,
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
        \Magento\Store\Model\StoreManagerInterface $storeManager
    ) {
        parent::__construct($context);
        $this->_transportBuilder = $transportBuilder;
        $this->inlineTranslation = $inlineTranslation;
        $this->scopeConfig = $scopeConfig;
        $this->storeManager = $storeManager;
    }

    /**
     * Dispatch request
     *
     * @param RequestInterface $request
     * @return \Magento\Framework\App\ResponseInterface
     * @throws \Magento\Framework\Exception\NotFoundException
     */
    public function dispatch(RequestInterface $request)
    {
        return parent::dispatch($request);
    }
}

/app/code/Namespace/ModuleName/view/frontend/templates/form.phtml

<h1>Hello</h1>

route.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="test" frontName="test" >
        <module name="Namespace_ModuleName" />
    </route>
</router>

6
  • 1
    namespace first letter should be capital Commented Jul 18, 2016 at 12:00
  • i wrote just for example. Thanks, i will edit it Commented Jul 18, 2016 at 12:02
  • can u please share code of frontend/route.xml
    – Amit Bera
    Commented Jul 18, 2016 at 12:12
  • Already done. Maybe i have problem with permission? Commented Jul 18, 2016 at 12:34
  • Try to remove var/generation folder and try again. If still not working then please give us full code of your controller not just the execute method Commented Jul 18, 2016 at 13:01

2 Answers 2

7

Your module have some error, please follow below steps to have it work,

Step1 : app/code/Test/Module/Block/Form.php

<?php 

namespace Test\Module\Block;

use Magento\Framework\View\Element\Template;

class Form  extends Template
{

}
?>

Step2: app/code/Test/Module/Controller/Index/Index.php

<?php 
namespace Test\Module\Controller\Index;
class Index extends \Magento\Framework\App\Action\Action
{
    public function execute()
    {


         $this->_view->loadLayout();

        $this->_view->renderLayout();

    }
}
?>

Step3: app/code/Test/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="module" frontName="module">
            <module name="Test_Module" />
        </route>
    </router>
</config>

Step4: app/code/Test/Module/etc/module.xml

<?xml version="1.0"?>
<!--
/**
 * Copyright © 2015  All rights reserved.
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
  <module name="Test_Module" schema_version="1.0.0" setup_version="1.0.0"/> 
</config>

Step4 : app/code/Test/Module/view/frontend/layout/module_index_index.xml

<?xml version="1.0"?>
<!--
/**
 * Copyright © 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<page layout="1column" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
    <referenceContainer name="content">
        <block class="Test\Module\Block\Form" name="form" template="Test_Module::form.phtml" />
    </referenceContainer>
       </body>
</page>

Step5: app/code/Test/Module/view/frontend/templates/form.phtml

<h1>Hello</h1>

Step6: app/code/Test/Module/registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Test_Module',
    __DIR__
);
?>

After running php bin/magento setup:upgrade it will show output@url localhost/magentoce27/index.php/module/index/index

3
  • thanks, its working. Can you tell where i miss? Commented Jul 18, 2016 at 13:26
  • I will need to cross check which file I changed as When I run it was showing 404 page and then I made few changes around to have it work. You can alsways accept and upvote ans if it helped you. Thanks in advance!
    – Rushvi
    Commented Jul 18, 2016 at 13:46
  • 7
    FYI it's regarded as bad practice to put ?> at the end of your php files. The reason is, if you accidentally put whitespace after the ?> then PHP will begin its response to the client. When Magento comes to set its own headers and response, it'll error, "Headers already sent" and you'll spend hours debugging it. Commented May 9, 2017 at 16:12
2

For me below code work. please try and let me know

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd" layout="1column">
  <body>
    <referenceContainer name="content">
        <block class="Trimantra\HelloWorld\Block\HelloWorld" name="helloworld" template="helloworld.phtml" />
    </referenceContainer>
  </body>
</page> 
2
  • for me not. I put some different modules, but have the same result - white page. But i can echo through execute() method and it displays in the browser Commented Jul 18, 2016 at 12:20
  • Please check your log file and check what exactly error you get. and update your question. Commented Jul 18, 2016 at 12:23

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.