Latest update: You can check and download my new sample extension at here : Thienphucvx_sample
It includes:
– Work well with latest magento 2 version.
– Setup and install sample data for simple blog database
– Sending a request and create new blog post.
– It also includes cache tags so you need to clear cache to see new post after creating.
Bonus :
Magento 2 debug extension. It will be useful for debug purpose
In this tutorial, we create new module : Inbusiness_Helloworld
So let’s create a module that simply echos “Hello World” in the content.
1/ First we will start with a module.xml file to declare with magento2 about new module :
app\code\Inbusiness\Helloworld\etc\module.xml
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd"> <module name="Inbusiness_Helloworld" schema_version="0.0.0.1" active="true"> <sequence> <module name="Magento_Core"/> <module name="Magento_Store"/> </sequence> <depends> <module name="Magento_Store"/> </depends> </module> </config>
2/ Create app\code\Inbusiness\Helloworld\etc\frontend\page_types.xml
<?xml version="1.0"?> <page_types xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../Core/etc/page_types.xsd"> <type id="helloworld_index_index" label="Hello World Demo Site"/> </page_types>
3/ Create app\code\Inbusiness\Helloworld\etc\frontend\routes.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/App/etc/routes.xsd"> <router id="standard"> <route id="helloworld" frontName="helloworld"> <module name="Inbusiness_Helloworld" /> </route> </router> </config>
4/ Create app\code\Inbusiness\Helloworld\Controller\Index.php
<?php
namespace Inbusiness\Helloworld\Controller;
use Magento\Framework\App\Action\NotFoundException;
use Magento\Framework\App\RequestInterface;
class Index extends \Magento\Framework\App\Action\Action
{
/**
* @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;
}
}
5/ Create app\code\Inbusiness\Helloworld\Controller\Index\Index.php
<?php
namespace Inbusiness\Helloworld\Controller\Index;
class Index extends \Inbusiness\Helloworld\Controller\Index
{
/**
* Show Hello World page
*
* @return void
*/
public function execute()
{
$this->_view->loadLayout();
$this->_view->renderLayout();
}
}
6/ Create app\code\Inbusiness\Helloworld\view\frontend\layout\helloworld_index_index.xml
<?xml version="1.0"?> <layout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../Core/etc/layout_single.xsd"> <update handle="page_one_column"/> <referenceBlock name="head"> <action method="setTitle"> <argument translate="true" name="title" xsi:type="string">Hello World Demo Site</argument> </action> </referenceBlock> <referenceContainer name="content"> <block class="Magento\Framework\View\Element\Template" name="helloworld" template="Inbusiness_Helloworld::helloworld.phtml"></block> </referenceContainer> </layout>
7/ Create app\code\Inbusiness\Helloworld\view\frontend\templates\helloworld.phtml
<?php $message = "Hello World"; echo $message; ?>
When you enter : your_site/helloworld, “Hello World” will be displayed.
Happy coding !
COMMENTS