← lessons

Create a custom route in Magento

Video Lesson: Create a custom route in Magento

Add a new route to your Magento store to handle specific URL patterns.

Mark Shust

TEACHER

Mark Shust

Published 3 months ago

Share with colleagues:

Share on X Share on LinkedIn

Lesson Content

Custom routes tell Magento how to handle specific URL patterns. They're perfect for adding new pages or functionality to your store. Let's create one that handles all URLs starting with /blog.

We need to create a route configuration file named routes.xml in our etc directory, placing it in either the frontend area (for storefront pages) or adminhtml area (for admin panel pages).

Since we're building a blog for customers to read, we'll create this in the frontend area:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <router id="standard">
        <route id="blog" frontName="blog">
            <module name="Macademy_Blog"/>
        </route>
    </router>
</config>

Within this file we start with our root config node — this tells Magento this is a configuration file. The schema location in the XML header will validate our route config.

Next, we add a router node with id="standard". This tells Magento to use its standard router, which handles most frontend URL requests in your store.

Inside the router, we define our specific route using the route node. This needs two key attributes:

  • frontName: defines the URL path we want to match (in this case, blog)

  • id: gives our route a unique identifier in the system (also blog)

Pro tip: Using dashes in your URLs? That's fine for the frontName, but the id attribute only allows letters, numbers, and underscores. So if your frontName is "my-blog", your id should be "my_blog"."

Finally, we add the module node to tell Magento which module should handle requests matching this route. We use our module's full name in the standard Vendor_Module format.

That's all you need! Now when someone visits /blog, Magento knows exactly where to send that request. You can now create controllers to handle different actions under this route.

Welcome University student! You have extended access.

Learn more about this topic in the Magento 2 Coding Kickstart course:

View course →

Want to learn more about this topic?

It's covered in great detail within the Magento 2 Coding Kickstart course.

View course →
M Bytes Newsletter
Bi-weekly Magento deep-dives

Watch. Read. Apply. One topic every other week.

Fresh bytes every other Thursday. No spam. Unsubscribe anytime.


Join 9,000+ developers learning Magento, one byte at a time.