I posted a question earlier but didn't able to understand the answer,

Magento 2: How to create a dynamic url with the same controller

i want to create a page with the dynamic url but with same controller to handle the requests,

for example: and

just able to get the id and display the results accoring to the id = "jatin123" display this user profile and id="amit123" display this user profile.

Please suggest a solution.

1 Answer 1


You can create it by with the help of Magento\Framework\App\RouterList. For this you need to create di.xml in your module (I am assuming that you know how to create module in M2). First create di.xml in below path,


In this file add

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Framework\App\RouterList">
            <argument name="routerList" xsi:type="array">
                <item name="cms" xsi:type="array">
                    <item name="class" xsi:type="string">YOURVENDOR\YOURMODULE\Controller\Router</item>
                    <item name="disable" xsi:type="boolean">false</item>
                    <item name="sortOrder" xsi:type="string">60</item>

Now create a YOURVENDOR\YOURMODULE\Controller\Router but make sure that It should Implements \Magento\Framework\App\RouterInterface

    namespace YOURVENDOR\YOURMODULE\Controller;

    class Router implements \Magento\Framework\App\RouterInterface

     * @var \Magento\Framework\App\ActionFactory
    protected $actionFactory;

     * Event manager
     * @var \Magento\Framework\Event\ManagerInterface
    protected $_eventManager;

     * Store manager
     * @var \Magento\Store\Model\StoreManagerInterface
    protected $_storeManager;

     * Config primary
     * @var \Magento\Framework\App\State
    protected $_appState;

     * Url
     * @var \Magento\Framework\UrlInterface
    protected $_url;

     * Response
     * @var \Magento\Framework\App\ResponseInterface
    protected $_response;

  public function __construct(
        \Magento\Framework\App\ActionFactory $actionFactory,
        \Magento\Framework\Event\ManagerInterface $eventManager,
        \Magento\Framework\UrlInterface $url,        
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Framework\App\ResponseInterface $response
    ) {
        $this->actionFactory = $actionFactory;
        $this->_eventManager = $eventManager;
        $this->_url = $url;        
        $this->_storeManager = $storeManager;
        $this->_response = $response;

     public function match(\Magento\Framework\App\RequestInterface $request)
        $identifier = trim($request->getPathInfo(), '/');

        $condition = new \Magento\Framework\DataObject(['identifier' => $identifier, 'continue' => true]);

        $identifier = $condition->getIdentifier();

        if ($condition->getRedirectUrl()) {
            return $this->actionFactory->create('Magento\Framework\App\Action\Redirect');

        if (!$condition->getContinue()) {
            return null;

        // check your custom condition here if its satisfy they go ahed othrwise set return null
        if (!$satisfy) {
            return null;

        $request->setModuleName('yourmodule')->setControllerName('yourcontroller')->setActionName('youraction')->setParam('custom_id',"test" );
        $request->setAlias(\Magento\Framework\Url::REWRITE_REQUEST_PATH_ALIAS, $identifier);

        return $this->actionFactory->create('Magento\Framework\App\Action\Forward');

Alternatively you can also take help from Magento_Cms module. Magento team also doing the same.





  • 1
    nice answer by @KeyurShah Commented Oct 12, 2017 at 10:42
  • what should be the your controller name
    – Jay Kapoor
    Commented Oct 12, 2017 at 10:52
  • Router.php as you can see class name is also Router so :) @JayKapoor
    – Keyur Shah
    Commented Oct 12, 2017 at 10:53
  • no, i asked this setControllerName('yourcontroller')
    – Jay Kapoor
    Commented Oct 12, 2017 at 10:55
  • What is your layout file ? let me give you an example for CMS , cms_page_view.xml cms is module name page is controller name and view is action . so it depends on layout @JayKapoor
    – Keyur Shah
    Commented Oct 12, 2017 at 11:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.