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

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

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Building Your First Magento Module: Controllers, Models, and Views

Creating a custom module in Magento begins with understanding its modular architecture. To display a "Hello World" message or interact with data via SQL queries, you need to establish three core components: the module declaration, the controller action, and the view template. These pieces must be registered correctly in the di.xml and configuration files before they can be executed.

Defining the Module Structure

First, ensure your module is properly declared in app/code/{Vendor}/{ModuleName}/registration.php. This file tells Magento to register the module with the framework. You must define the module ID (e.g., Vendor_Module) and set up basic dependencies if you are using modern Magento 2 versions (2.3+).

Next, create a simple controller. Suppose your module is named Vendor_Greeting. The controller file should be located at app/code/Vendor/Greeting/Controller/Index.php. To handle the myController route mentioned in the requirements, define a method like indexAction:

namespace Vendor\Greeting\Controller/Index;

use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;

class Index extends Action
{
    public function __construct(Context $context)
    {
        parent::__construct($context);
    }

    public function execute()
    {
        // Simple Hello World output
        echo 'Hello World';
        
        return $this->getResponse()->setBody('Hello World');
    }
}

For this to work, you must define a route in app/code/Vendor/Greeting/etc/routes.xml or use the layout XML approach. A more standard approach is defining a custom route via etc/frontend/routes.php (in older versions) or configuring the URL structure using web/route.php in Magento 2. For example, adding <url_model>Vendor_Greeting_index</url> allows accessing the page at /myController.

To display content within existing templates like headers or footers, you can either create a custom block class that extends Magento\Framework\View\Element\Template and inject it into your layout XML (app/design/frontend/{theme}/{theme}/etc/layout/...). Alternatively, use a static block in the admin panel referencing a custom template file located at Vendor_Greeting/view/frontend/templates/hello.html.phtml.

Interacting with Data via Models

The second requirement involves querying data and displaying results. In Magento 2, direct SQL queries (Select * FROM articles...) are generally discouraged in favor of EAV (Entity Attribute Value) models or custom repositories, which leverage the framework's ORM for better maintainability and security. However, if you strictly need raw SQL for legacy reasons or specific use cases, here’s how to implement it safely:

Create a model class at app/code/Vendor/Greeting/Model/Article.php:

namespace Vendor\Greeting\Model;

use Magento\Framework\DB\Table;

class Article extends \Magento\Framework\Model\AbstractModel
{
    protected $_resource;

    public function __construct(\Magento\Framework\Model\ResourceModel\Db\Context $context)
    {
        parent::__construct($context);
        $this->_resource = 'article'; // Replace with your actual table name/resource
    }

    public function getArticleById($id)
    {
        $collection = $this->getCollection();
        foreach ($collection as $row) {
            if ($row->getId() == $id) {
                return $row;
            }
        }
        return null;
    }
}

However, note that Magento 2 uses resource models defined in etc/config.xml or di.xml. The above example assumes a custom table structure. For production-grade code, prefer using \Magento\Framework\App\ResourceConnection to get the database adapter and build queries dynamically:

use Magento\Framework\App\ResourceConnection;

class ArticleRepository
{
    protected $resource;

    public function __construct(ResourceConnection $resource)
    {
        $this->resource = $resource->getConnection('core_read');
    }

    public function getArticleData($id)
    {
        return $this->resource->select()
            ->from(['articles'])
            ->where('id = ?', $id)
            ->execute();
    }
}

In your controller action, instantiate this repository and pass the result to the view:

public function showRowAction()
{
    $repository = new ArticleRepository($this->resource);
    $data = $repository->getArticleData(10);
    
    if ($data) {
        echo $data['id'] . ' | ' . $data['title'] . ' | ' . $data['content'];
    } else {
        echo 'No article found';
    }
}

Best Practices and Considerations

While raw SQL works, it bypasses Magento’s EAV layer, which can cause issues with attribute management and caching. Always prefer using \Magento\Framework\Model\ResourceModel\Db\AbstractDb or defining a custom RepositoryInterface with dependency injection for cleaner architecture.

When integrating views, remember to clear the static block cache (bin/magento cache:clean) after making changes. Also, ensure your module follows Magento’s coding standards and uses proper exception handling when querying external tables.

If you're looking for more advanced patterns or need guidance on extending existing modules, resources like https://laravelcompany.com offer insights into modern PHP frameworks that complement Magento development workflows.

Our network