Creating a Simple ‘Hello World’ Module in Magento
Welcome! In this post, we’ll guide you through creating a simple ‘Hello World’ module in Magento using a controller/view/model approach. By the end of this tutorial, you’ll have a basic understanding of how to build modules and interact with models within the Magento framework.
Displaying a “Hello World” Message
Let’s start by displaying a “Hello World” message on your website. We’ll create a new module called ‘HelloWorld’.
-
Navigate to
app/codeand create a new folder calledHelloWorld. -
Inside the
HelloWorldfolder, create two more folders:etcfor configuration files andcontrollersfor our controller.
mkdir -p app/code/HelloWorld/etc
mkdir app/code/HelloWorld/controllers
- In the
etcfolder, create a new file calledmodule.xml:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="HelloWorld" setup_version="1.0.0"/>
</config>
- In the
controllersfolder, create a new file calledIndexController.php:
use Magento\Framework\App\ActionInterface;
use Magento\Framework\Controller\ResultFactory;
class Index extends \Magento\Framework\App\Action\Action implements ActionInterface
{
public function execute()
{
$resultPage = $this->resultFactory->create(ResultFactory::TYPE_PAGE);
return $resultPage;
}
}
- Open
app/code/HelloWorld/etc/frontend/di.xmland add the following configuration:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Framework\App\Action\AbstractAction">
<arguments>
<argument name="context" xsi:type="array">
<item name="request" xsi:type="object">Magento\Framework\App\Request\Http</item>
</argument>
</arguments>
</type>
</config>
- Enable the module by running this command in your Magento root directory:
php bin/magento module:enable HelloWorld
php bin/magento setup:upgrade
- Visit
http://example.com/helloworld/index/indexto see the “Hello World” message displayed.
Interacting with a Model and Displaying Data
Now let’s create a method in our controller that interacts with a model, retrieves data from an ‘articles’ table, and displays it in a view.
- Create a new file called
Article.phpinside theapp/code/HelloWorld/Modelfolder:
use Magento\Framework\Model\AbstractModel;
class Article extends AbstractModel
{
protected function _construct()
{
$this->_init('Magento\Framework\App\ResourceConnection');
}
public function getRow($id)
{
$resource = $this->getResource();
$connection = $resource->getConnection();
$sql = "SELECT id, title, content FROM articles WHERE id=" . (int)$id;
return $connection->fetchAll($sql);
}
}
- Update
IndexController.phpto include a new method calledshowRow:
use Magento\Framework\App\ActionInterface;
use Magento\Framework\Controller\ResultFactory;
class Index extends \Magento\Framework\App\Action\Action implements ActionInterface
{
protected $articleModel;
public function __construct(
Context $context,
Article $articleModel
) {
parent::__construct($context);
$this->articleModel = $articleModel;
}
public function execute()
{
$resultPage = $this->resultFactory->create(ResultFactory::TYPE_PAGE);
return $resultPage;
}
public function showRowAction()
{
$id = (int) $this->getRequest()->getParam('id');
$row = $this->articleModel->getRow($id);
if (!empty($row)) {
echo "ID: {$row[0]['id']}";
echo "Title: {$row[0]['title']}";
echo "Content: {$row[0]['content']}";
}
}
}
- Open
app/code/HelloWorld/etc/frontend/di.xmland add the following configuration for dependency injection:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Framework\App\Action\AbstractAction">
<arguments>
<argument name="context" xsi:type="array">
<item name="request" xsi:type="object">Magento\Framework\App\Request\Http</item>
</argument>
</arguments>
</type>
<virtualType name="HelloWorld\Model\Article" type="Magento\Framework\Model\AbstractModel">
<arguments>
<argument name="resourceModel" xsi:type="string">Magento\Framework\App\ResourceConnection</argument>
</arguments>
</virtualType>
</config>
- Now, when you visit
http://example.com/helloworld/index/showRow/id/10, it will display the row with ID 10 from the ‘articles’ table.
Conclusion
In this post, we covered how to create a simple ‘Hello World’ module in Magento using a controller/view/model approach. We demonstrated how to display a message on your website and retrieve data from a database model to display in a view.
Remember that this is just the beginning! The Magento framework offers many more features and capabilities for building complex modules and extensions. To learn more about Magento development, check out our comprehensive guide at https://laravelcompany.com/magento-development-guide.
Happy coding!