2

My module was working perfectly in Magento2 Beta version. In stable version, module is getting listed and custom module backend is working fine. However frontend pages of my module are not working. Here is my code.

Index Controller

namespace Companyname\Modulename\Controller\Index;

use Magento\Framework\App\Action\Action;

class Index extends Action {
    public function execute() { 
        $this->_view->loadLayout();
        $this->_view->renderLayout();   
    }

}

Layout - /app/code/Companyname/Modulename/view/frontend/layout/modulename_index_index.xml

<?xml version="1.0"?>
<page 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="Companyname\Modulename\Block\Modulename" name="modulename" template="Companyname_Modulename::modulename.phtml" />
    </referenceContainer>        
    </body>
</page>

Block File

namespace Companyname\Modulename\Block;
class Modulename extends \Magento\Framework\View\Element\Template {
   public function __construct(
       \Magento\Framework\View\Element\Template\Context $context, 
        array $data = array()
    ) {     
        parent::__construct($context, $data);
   }    
}

When i debug, it does not come in construct of the block. I am assuming that it is not able to find the block. The phtml file simply prints echo "hello world"; But nothing appears on frontend. Any ideas as to what i am doing wrong ?

2

I think issue in your modulename_index_index.xml file, You have to mention page layout like layout="1column".

You can change your page layout

  • 1column
  • 2columns-left
  • 2columns-right
  • 3columns
2

You need to specify the layout for the page.
So add this layout="2columns-left" on the <page> node in hello_index_index.xml.
You can also use 1column, 2columns-right, 3columns or empty.

0

Please make sure that your controller is executed, if not check routes.

Additionally, layout is not supposed to be manipulated directly via view object in controller (like it was in Magento 1), result page object should be created instead.

Please consider rewriting your controller as follow:

<?php
namespace CompanyName\ModuleName\Controller\Index;

use Magento\Framework\View\Result\PageFactory;
use Magento\Framework\App\Action\Context;

class Index extends \Magento\Framework\App\Action\Action
{
    /**
     * @var PageFactory
     */
    protected $resultPageFactory;

    /**
     * @param Context $context
     * @param PageFactory $resultPageFactory
     */
    public function __construct(
        Context $context,
        PageFactory $resultPageFactory
    ) {
        $this->resultPageFactory = $resultPageFactory;
        parent::__construct($context);
    }

    /**
     * Some action.
     *
     * @return \Magento\Framework\View\Result\Page
     */
    public function execute()
    {
        /** @var \Magento\Framework\View\Result\Page $resultPage */
        $resultPage = $this->resultPageFactory->create();
        return $resultPage;
    }
}
  • Is your request coming to Index controller ? – Keyur Shah Nov 24 '15 at 10:09
  • Can you please load your block name using $this->_view->getLayout() ->getBlock('modulename'); after $this->_view->loadLayout(); – Keyur Shah Nov 24 '15 at 10:23
  • have you tried using clear cache ? @ReenaParekh – Keyur Shah Nov 24 '15 at 10:27
  • @ReenaParekh Regarding blank page, unfortunately you are right, it is still not possible to avoid inheritance from action controller, but you still should use result page object. Updated example. – Alex Paliarush Nov 24 '15 at 10:32
  • Try to clear var/generation, most likely you have old proxy there. Also, have you cleared pub/static and the whole var/* ? – Alex Paliarush Nov 24 '15 at 10:46

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.