Create a Magento module skeleton, route, and controller

In this lesson, we will create a new Magento module, set up a route, and define a controller.

Creating the Module Files

  1. First, navigate to the app/code folder and create a new file at Macademy/InventoryFulfillment/registration.php.
  2. Next, create the module definition file at etc/module.xml and populate it with the necessary code.

Setting Up the Route

  1. Now, set up a new route under etc/frontend/routes.xml and define a new routes file. This will use the “standard” router, and the frontName for our route will be inventory-fulfillment.

Creating the Controller

  1. To define a new controller when we land on the /inventory-fulfillment route, create a new file under our module route named Controller/Index/Index.php.
  2. Bootstrap the file with code for a standard controller. This will create a new class that implements the HttpGetActionInterface, create a pageFactory object, and return an instance of that factory as a Page object.
  3. Modify the execute method by hoisting the pageFactory->create() into its own $page variable and return this instance.
  4. Set a custom title for this page by calling $page->getConfig()->getTitle(), and then >set(). Pass the title through the translation function to make it translatable. The title of this page will be “Shipping Plan”.

Enabling the Module

Enable the module with bin/magento module:enable Macademy_InventoryFulfillment, which is the name of our module.

Register the module with Magento by executing bin/magento setup:upgrade. This clears any caches and generated code to ensure our module is fully enabled.

Testing the Route

After executing the previous steps, visit the store and go to the route at /inventory-fulfillment. You should see a successful response, with the name of our page, “Shipping Plan”, appearing in the page title and the meta title. This confirms that everything has been successfully set up with our module.

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