0

I have to extend functionality of Magento_Customer module and new some more controller under customer/account like customer/account/mobileVerification so my question is, Is okay to have the same frontName customer for another Module Vendor_Customer in app/code/Vendor/Customer/etc/frontend/routes.xml.

Will it create issues ? or is it okay to do so.

3
  • No you can not create like that Jan 29, 2018 at 6:37
  • @NileshGosai can you reason why ? as far as working is concern the url is accessible example.com/customer/account/mobileVerification from browser. Jan 29, 2018 at 6:39
  • I have given the answer please do like that Jan 29, 2018 at 7:09

2 Answers 2

4

Please follow below steps:

Step-1: go to "app" from root directory of Magento 2 and create new directory code. Then create two more directories in app/code, Namespace and Module Name. The final directory will look like this: app/code/Demo/MobileVerification

Demo as Namespace and MobileVerification as module name.

Please find below Steps:

Step-2: create "registration.php" file in app/code/Demo/MobileVerification/ and paste below code into the file:

 <?php
    \Magento\Framework\Component\ComponentRegistrar::register(
        \Magento\Framework\Component\ComponentRegistrar::MODULE,
        'Demo_MobileVerification',
        __DIR__
    );

Step-3: create "module.xml" file in app/code/Demo/MobileVerification/etc and paste below code into the file:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Demo_MobileVerification" setup_version="1.0.0">
        <sequence>
            <module name="Magento_Customer"/>
        </sequence>
    </module>
</config>

Step-4: create "route.xml" file in app/code/Demo/MobileVerification /etc/frontend and paste below code into the file:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <router id="standard">
        <route id="mobileverification" frontName="mobileverification">
            <module name="Demo_MobileVerification" />
        </route>
    </router>
</config>

Step-5: create "di.xml" file in app/code/Demo/MobileVerification /etc/frontend and paste below code into the file:

<?xml version="1.0"?>
<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">
        <arguments>
            <argument name="routerList" xsi:type="array">
                <item name="mobileverification " xsi:type="array">
                    <item name="class" xsi:type="string">Demo\MobileVerification\Controller\Router</item>
                    <item name="disable" xsi:type="boolean">false</item>
                    <item name="sortOrder" xsi:type="string">60</item>
                </item>
            </argument>
        </arguments>
    </type>
</config>

Step-6: create "Router.php" file in app/code/Demo/MobileVerification /Controller and paste below code into the file:

<?php
namespace Demo\MobileVerification\Controller;


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

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

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

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

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


    /**
     * @param \Magento\Framework\App\ActionFactory $actionFactory
     * @param \Magento\Framework\Event\ManagerInterface $eventManager
     * @param \Magento\Framework\UrlInterface $url
     * @param \Magento\Framework\App\ResponseInterface $response
     */
    public function __construct(
        \Magento\Framework\App\ActionFactory $actionFactory,
        \Magento\Framework\Event\ManagerInterface $eventManager,
        \Magento\Framework\UrlInterface $url,
        \Magento\Framework\App\ResponseInterface $response
    ) {
        $this->actionFactory = $actionFactory;
        $this->_eventManager = $eventManager;
        $this->_url = $url;
        $this->_response = $response;
    }

    /**
     * Validate and Match MobileVerification Page and modify request
     *
     * @param \Magento\Framework\App\RequestInterface $request
     * @return bool
     */
     public function match(\Magento\Framework\App\RequestInterface $request)
     {
        $url_key = trim($request->getPathInfo(), '/');
        if($url_key == "customer/mobileverification/index")
        {
            $request->setModuleName('mobileverification')->setControllerName('index')->setActionName('index');
            $request->setAlias(\Magento\Framework\Url::REWRITE_REQUEST_PATH_ALIAS, $url_key);
            return $this->actionFactory->create('Magento\Framework\App\Action\Forward');    
        }
        else 
        {
            return;
        }
    }
}

Step-7: create "Index.php" file in app/code/Demo/MobileVerification /Controller/Index and paste below code into the file:

<?php
namespace Demo\MobileVerification\Controller\Index;

class Index extends \\Magento\Framework\App\Action\Action
{
    public function execute()
    {
        $resultPage = $this->resultPageFactory->create();
        $resultPage->getConfig()->getTitle()->set(__('Mobile Verification'));
        return $resultPage;
    }
}

Step-8: Block Class (Demo\MobileVerification\Block\MobileVerification), Layout xml(Demo\MobileVerification\view\frontend\layout\mobileverification_index_index.xml) and template (Demo\MobileVerification\view\frontend\template) file as per your requirement.

Hope this helps...

2
3

I think Nilesh Gosai answer is over engineering in this case.

You can easily create file:

{VendorName}/{ModuleName}/etc/frontend/routes.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <router id="standart">
        <route id="customer" frontName="customer">
            <module name="{VendorName}_{ModuleName}" before="Magento_Customer" />
        </route>
    </router>
</config>

And your controller: {VendorName}/{ModuleName}/Controller/Adminhtml/Account/MobileVerification.php

And get access by url http://example.com/customer/account/mobileVerification

See in documentation: https://devdocs.magento.com/guides/v2.4/extension-dev-guide/routing.html#before-and-after-parameters

0

Your Answer

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

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