1

How to cut short the URL of a controller action page in magento2

For instance:

If custom modules controller page is in : Vendor/Module/Controller/Index/Index.php

in routes.xml: I had defined frontname as : i

Instead of navigating to : localhost/magento/i/index/index

How to optimize the URl to only localhost/magento/i

Basically , i need to achieve this (as in magento1) in Magento2.

<!-- url rewrite -->
<rewrite>
<mypage>
    <from><![CDATA[#^/([\w-]+)/m/?#]]></from>
    <to><![CDATA[/m/index/index/product/$1]]></to>
    <complete>1</complete>
</mypage>
</rewrite>
<!-- end url rewrite -->
<frontend>
<routers>
<mypage>
  <use>standard</use>
    <args>
       <module>Vendor_Module</module>
       <frontName>m</frontName>
    </args>
  </mypage>
 </routers>

For the above rewrite : we can get the controller action just by:

localhost/magento/m

Need to achieve the same in Magento2 ?

UPDATE:

<?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="m" frontName="m">
        <module name="Vendor_Module"/>
    </route>
</router>

Controller path: \Vendor\Module\Controller\Result\Index.php

layout file name: m_result_index

URL: localhost/magento/m

I get 404 no found.

Cleared cache and gen

1

1 Answer 1

0

Define frontName as i in routes.xml

Add file i_index_index.xml in yourmodule/frontend/layout

Now you can directly access localhost/magento/i

7
  • Please find the updated answer, i tried the above, i get 404
    – Sushivam
    Commented Oct 5, 2016 at 7:16
  • @SachinS I have tested from my side, it's working for me Commented Oct 5, 2016 at 7:29
  • with just localhost/magento/i from my code? is ur controller action loading?
    – Sushivam
    Commented Oct 5, 2016 at 7:32
  • Yes exactly localhost/mymagento2/myroute calls myroute/index/index Commented Oct 5, 2016 at 7:37
  • @SachinS Your frontName must be atleast tree character long Commented Oct 5, 2016 at 7:42

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.