Custom Router in Magento: define Custom URLs and Routes

Creating a custom router in Magento allows you to define how URLs are processed and routed within your Magento-based e-commerce website. This can be useful when you want to create custom URLs for specific pages or actions. Here’s a step-by-step guide on how to create a custom router in Magento:

  1. Create a Custom Module:

    First, you need to create a custom module if you haven’t already. You can do this by creating a directory structure in the app/code directory, for example: app/code/YourCompany/YourModule.

  2. Create a Router Configuration File:

    Inside your module directory, create a etc/frontend folder if it doesn’t already exist, and then create a routes.xml file. This file will define your custom router. Here’s an example of what the content might look like:

    
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
        <router id="custom_router" frontName="yourcustomroute">
            <route id="custom" frontName="custom">
                <module name="YourCompany_YourModule" before="Magento_Cms" />
            route>
        router>
    config>
    

    In this example, we define a custom router with the frontName “yourcustomroute,” and it’s configured to use the module “YourCompany_YourModule.”

  3. Add to etc/frontend/di.xml 

    xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">




    Vendor\Module\Controller\Router
    false
    22




  4. Create a Controller:

    Create a controller that will handle the requests to your custom route. You can place your controller file in the Controller directory of your module. The controller should be named something like YourController.php. Here’s an example of a simple controller:

    
    namespace YourCompany\YourModule\Controller\Custom;
    
    class YourController extends \Magento\Framework\App\Action\Action
    {
        public function execute()
        {
            // Your controller logic goes here
        }
    }
    
  5. Create a router and add routing logic into match function

    class Router implements RouterInterface
    public function match(RequestInterface $request): ?ActionInterface

     

  6. Custom URL Structure:

    Now, you can access your custom route using a URL like http://yourdomain.com/yourcustomroute/controllername. In the example above, “controllername” should be replaced with the name of your controller file without the “.php” extension.

  7. Flush Cache:

    After creating a custom router, remember to flush the cache to make Magento aware of your changes. You can do this via the command line with bin/magento cache:flush.

  8. Test Your Custom Router:

    Visit your custom URL in a web browser to test the custom router.

This is a basic example of creating a custom router in Magento. Depending on your specific requirements, you may need to configure your custom router differently and implement more complex logic in your controller. Additionally, consider security best practices when creating custom routes to ensure the integrity and security of your Magento store.