Question

I am trying to redirect a cms page to a new route which I have created for frontend. I am not sure if we can redirect to the new custom module route frontend with a config in admin area. My aim is I have a contact page in CMS and I want to enable the custom module to show only if the module is enabled if not show the CMS page

Was it helpful?

Solution

Yes, that can be possible.

I hope you talking about route, not router.

See https://devdocs.magento.com/guides/v2.4/extension-dev-guide/routing.html#custom-routers

First, create a custom router make sortOrder field between 31 to 59.

Using

Magento manage routers execution by sortOrder file Cms page router sort order is 60,

See Router Sorting from https://devdocs.magento.com/guides/v2.3/extension-dev-guide/routing.html#router-class

Code Solution

Create a custom router which sortOrder is 40.

Declare custom router at di.xml

File location: app/code/{Vendorname}/{Modulename}/etc/frontend

Code

<?xml version="1.0" ?>
<type name="Magento\Framework\App\RouterList">
    <arguments>
        <argument name="routerList" xsi:type="array">
            <item name="my_custom_router" xsi:type="array">
                <item name="class" xsi:type="string">{Vendorname}\{Modulename}\Controller\Router</item>
                <item name="disable" xsi:type="boolean">false</item>
                <item name="sortOrder" xsi:type="string">40</item>
            </item>
        </argument>
    </arguments>
</type>

Defined Router Class

  • FileName: Router.php

  • location: app/code/{Vendorname}/{Modulename}/Controller/

and code:

<?php


namespace Ronin\ErpOrder\Controller;


use Magento\Framework\App\ActionInterface;
use Magento\Framework\App\RequestInterface;

class Router implements \Magento\Framework\App\RouterInterface
{
    /**
     * @var \Magento\Framework\App\ActionFactory
     */
    private $actionFactory;
    /**
     * @var \Magento\Framework\App\ResponseInterface
     */
    private $response;

    public function __construct(
        \Magento\Framework\App\ActionFactory $actionFactory,
        \Magento\Framework\App\ResponseInterface $response
    ) {
        $this->actionFactory = $actionFactory;
        $this->response = $response;
    }

    public function match(RequestInterface $request)
    {
        /**
         *  If your  request url is https://www.amitbera.com/xyz/abc
         *  Then $identifier give xyz/abc Means
         *
         */

        $identifier = trim($request->getPathInfo(), '/');

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

        /**
         * Url Check at here
         */
        if($condition === {$MatchThen}){
            /**
             * If Url match with Your logic then assign route,Controller,Action
             *
             */
            $request->setModuleName('{RouteId}')->setControllerName('{Contorllername}')
                ->setActionName('{Actionname}');
            $request->setAlias(\Magento\Framework\Url::REWRITE_REQUEST_PATH_ALIAS, $identifier);
            return $this->actionFactory->create(\Magento\Framework\App\Action\Forward::class);

        }
        return null;
    }
}

Checkout vendor/magento/module-cms/Controller/Router.php how magento implement check URL and match with request and assign route id, controller,Action.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top