Dynamic Route or Custom Route for Magento 2

To enable dynamic route in the Magento 2, developers can create a custom Magento 2 Router class, which is commonly used in extensions such as blog modules. This allows merchants to configure the URL of the page within the Stores -> Configuration area in the admin panel. Here are the steps to implement this functionality:

Note: We keep it simple so we are not going to set route name using store configuration

Begin by adding the necessary files for your custom module, including registration.php and module.xml. These files are essential for registering and configuring your module in the Magento 2 system.

Note: We are using Module name as “NdCode_CustomRouter" for this extension

To configure the custom Router use di.xml file. This step ensures that Magento recognizes your Router class as the active router for your module. Define the router class in the appropriate configuration section of the di.xml file

Implement the match method in your Router class. This method will be responsible for matching the requested URL with the defined route and returning the appropriate controller and action.

Define the route: Inside your module, create a routes.xml file in the etc/frontend

Inside your controller, implement the action method that will handle the logic for your dynamic route. This method will execute when the route is accessed. You can add your custom business logic, load necessary data, and define the response.

You see we are setting title for now as example

Create the layout file: To define the layout for your dynamic route, create a layout XML file in your module’s view/frontend/layout directory (or view/adminhtml/layout for the admin panel). In this file, specify the block, template, and any other layout elements required for your page.

Create the template file: Create the template file that will be used as the rendering template for your dynamic route. This file should be placed in your module’s view/frontend/templates directory

These steps provide a general overview of adding a dynamic route in the Magento 2 admin. you can refer to the original article or official Magento documentation:

https://developer.adobe.com/commerce/php/development/components/routing/#custom-routers

Also, you can download entire module from here
https://github.com/nd1996/dynamic-router-magento-2

You all are the semicolon to my statements; Please support me; Thanks