How do I create a simple 'Hello World' module in Magento?

How do I create a simple 'Hello World' module in Magento?

July 29, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How do I create a simple 'Hello World' module in Magento?

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’.

  1. Navigate to app/code and create a new folder called HelloWorld.

  2. Inside the HelloWorld folder, create two more folders: etc for configuration files and controllers for our controller.

mkdir -p app/code/HelloWorld/etc
mkdir app/code/HelloWorld/controllers
  1. In the etc folder, create a new file called module.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>
  1. In the controllers folder, create a new file called IndexController.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;
    }
}
  1. Open app/code/HelloWorld/etc/frontend/di.xml and 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>
  1. Enable the module by running this command in your Magento root directory:
php bin/magento module:enable HelloWorld
php bin/magento setup:upgrade
  1. Visit http://example.com/helloworld/index/index to 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.

  1. Create a new file called Article.php inside the app/code/HelloWorld/Model folder:
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);
    }
}
  1. Update IndexController.php to include a new method called showRow:
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']}";
        }
    }
}
  1. Open app/code/HelloWorld/etc/frontend/di.xml and 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>
  1. 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!

Indexing Extensions Configuration

Our network