Create a controller action in Magento

Even though we have our module set up to handle web requests for the blog URL, if we try to load the /blog URL, a 404 error page will be returned. This is because even though our route is set up, a controller must also exist to be able to handle the incoming request.

A controller is just a regular PHP class. Magento looks for controllers in the Controller folder of a module, so let’s go ahead and create it. Be very sure to capitalize the C in Controller, otherwise you’ll have all sorts of odd errors eventually on case sensitive operating systems.

It’s a good time to remember that Magento is a Model-View-Controller framework, and it definitely follows an MVC structure in this Controller folder. This means that folders and subfolders in this directory are named very specifically.

Thinking back to our URL anatomy lesson, the URL has a front name, controller name and action name. Since the controller name doesn’t exist for /blog, it defines to index. This is the equivalent of typing /blog/index.

Let’s take this index name and convert it to TitleCase, and create a folder with that name. This means that we need to create a folder named Index.

Back to the URL, the action name also doesn’t exist, but this again defaults to index. Let’s create a new PHP file under our Index folder, but this PHP file will reference our action name in TitleCase format again. This will be Index.php.

Let’s open our PHP tag and declare strict types. Magento uses PHP namespaces, which is just a way to organize our code so it doesn’t pollute the global namespace. Our namespace is Macademy\Blog\Controllers\Index

Controller/Index/Index.php

<?php declare(strict_types=1);

namespace Macademy\Blog\Controller\\Index;

use Magento\Framework\App\Action\HttpGetActionInterface;

class Index implements HttpGetActionInterface
{
    public function execute()
    {
        
    }
}

Then we will define our class, Index, and Magento controllers implement an action interface. Then we will implement the HttpGetActionInterface, which is the interface used for HTTP GET requests.

If you don’t know about PHP interfaces, to sum it up, they are basically just blueprints that define which methods you need to implement. So if we go into this interface by Cmd+Clicking into it, or Ctrl+Click on Windows/Linux, we can inspect what it requires. In HttpGetActionInterface, we’ll see this extends HttpHeadActionInterface. If we go into this, we can see that this is actually deprecated, and it is extending the base ActionInterface, so this is the interface we are truly extending.

When we scroll down in this interface, we’ll see that it defines an execute() function. This is the only function we will need to implement in order to satisfy the requirements of the interface. So let’s close all of these files, and go back to our class to implement this execute() function.

Let’s create a public function named execute(). This is the function that is responsible for handling this web request.

Within this execute method, let’s just die out a response that tells us this is the “Blog index” route.

<?php declare(strict_types=1);

namespace Macademy\\Blog\\Controller\\Index;

use Magento\\Framework\\App\\Action\\HttpGetActionInterface;

class Index implements HttpGetActionInterface
{
    public function execute()
    {
        die('Blog index');
    }
}

When we refresh the page, we’ll see that this controller is in fact handling the response for this request. This isn’t the proper way to send a request, but we’ll need to learn a little about dependency injection before learning that.

Remember that this same URL is accessible not only at https://magento.test/blog/ but also at https://magento.test/blog/index, as well as and https://magento.test/blog/index/index

If for some reason it is still showing the 404 error page, try clearing the Magento cache with bin/magento cache:clean.

Complete and Continue  
Extra lesson content locked
Enroll to access all lessons, source code & comments.
Enroll now to Unlock