Building a Module in Magento 2 Part 2

July 25, 2018
keya

In the last blog, I talked about how to setup Magento 2 and introduce you to the basic structure of Magento Module creation and routing in Magento 2. In this blog, we will explore further into blocks and templates to display something in the frontend.

The first step let’s modify the SayHello.php controller that we implemented in the previous blog.

app/code/Demo/HelloWorld/Controller/Index/SayHello.php

<?php
namespace Demo\HelloWorld\Controller\Index;
use Magento\Framework\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;

class SayHello extends \Magento\Framework\App\Action\Action
{
protected $_pageFactory;
public function __construct(
Context $context,
PageFactory $pageFactory
){
$this->_pageFactory = $pageFactory;
parent::__construct($context);
}

public function execute(){
return $this->_pageFactory->create();
}
}

The PageFactory should be created inside the execute method to render the view.
Next step is to create the layout. The layout is located inside {module_root}/view/{area}/layout/ folder. The Area in the path can be frontend or adminhtml which define where the layout will be applied. There is a special layout file name default.xml which will be applied for all the page in its area. Otherwise, the layout file will have name as format: {router_id}_{controller_name}_{action_name}.xml. In our case, the path layout is located in Demo/HelloWorld/view/frontend/layout/ and the name of the layout file is helloworld_index_sayhello.xml.
Edit the helloworld_index_sayhello.xml with the following code.

<?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">
<referenceContainer name="content">
<block class="Demo\HelloWorld\Block\SayHello" name="helloworld_sayhello" template="Demo_HelloWorld::sayhello.phtml" />
</referenceContainer>
</page>

Next step is to create the block (Demo\HelloWorld\Block\SayHello) which would contain all the view logic.

<?php
namespace Demo\HelloWorld\Block;
use Magento\Framework\View\Element\Template\Context;

class SayHello extends \Magento\Framework\View\Element\Template
{
public function __construct(Context $context){
parent::__construct($context);
}

public function sayHello(){
return __('Hello World');
}
}

Every block in Magento 2 must extend from Magento\Framework\View\Element\Template. In this block, we will define a method sayHello() to show the word “Hello World”.
The template file should be located in {module_root}/view/{area}/templates/ . In our example, Demo/HelloWorld/view/frontend/templates/sayhello.phtml is the location of the template file and the name of the file should be sayhello.phtml that was mentioned in helloworld_index_sayhello.xml

<?php
/**
* @var \Demo\HelloWorld\Block\SayHello $block
*/
$greetings = $block->sayHello();
?>
<h1><?php echo $greetings ?></h1>

In the template file, we can use the variable $block for the block object. As you see, we call the method sayHello() in Block.

Now inside your project directory open the terminal and run the following command:

sudo php bin/magento setup:upgrade
sudo php bin/magento setup:di:compile
sudo php bin/magento setup:static-content:deploy
sudo php bin/magento cache:clean
sudo chmod -R 777 .

*For development mode, I like to give all 777 permission which is not permissible in production server.
Open your browser and visit the following URL: http://<yourhost.com>/helloworld/index/sayhello. You will see “Hello World” output.

Further Reference for Layout Structure Magento 2:

https://devdocs.magento.com/guides/v2.2/frontend-dev-guide/layouts/layout-overview.html
https://medium.com/@ishtiaque05/building-a-module-in-magento-2-part-1-fc08da0befae

Contributor: Syed Ishtiaque AhmadNascenia

No comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.