2

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

1

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.

| improve this answer | |
  • Thank you for this @Amit I meant route :) is there any example I could use to guide me this. I have looked at the links you have send me I can see sort order 60 is cms page but I want a guide if you know or show me. – Jack Brooks Jun 6 at 15:48
  • So,you are talking about router? – Amit Bera Jun 7 at 5:58
  • Yes I am talking about router, I will test this controller I really appreciate this showing me a example really nice of you. – Jack Brooks Jun 7 at 19:29
  • Amit question about the match function, just to get my head around what is routeID controllerNams and ActionName you doing a check if the condition match – Jack Brooks Jun 8 at 8:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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