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
- First, navigate to the
app/code
folder and create a new file atMacademy/InventoryFulfillment/registration.php
. - Next, create the module definition file at
etc/module.xml
and populate it with the necessary code.
Setting Up the Route
- 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 beinventory-fulfillment
.
Creating the Controller
- To define a new controller when we land on the
/inventory-fulfillment
route, create a new file under our module route namedController/Index/Index.php
. - Bootstrap the file with code for a standard controller. This will create a new class that implements the
HttpGetActionInterface
, create apageFactory
object, and return an instance of that factory as aPage
object. - Modify the
execute
method by hoisting thepageFactory->create()
into its own$page
variable and return this instance. - 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.
Enroll to access all lessons, source code & comments.
Enroll now to Unlock