1

In Magento 2:

I Want to change this route/URL in myproject from http://myproject.com/style/index/index/parameter/paravalue

to http://myproject.com/style/parameter/paravalue

This is a custom Url

I have added the route in XML file i.e routes.xml

          <route id="style" frontName="style">
              <module name="modulename"/>
          </route>

n i have a layout file style_index_index.xml where the template file is included.

/app/vendor/module/etc/di.xml

   <?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="style" xsi:type="array">
              <item name="class" xsi:type="string">vendor\module\Controller\Style</item>
              <item name="disable" xsi:type="boolean">false</item>
              <item name="sortOrder" xsi:type="string">22</item>
          </item>
      </argument>
  </arguments>
 </type>
 </config>

Created the app/vendor/module/controller/Style.php

   namespace vendor\module\Controller;

    class Style implements \Magento\Framework\App\RouterInterface
    {
        protected $actionFactory;
        protected $_response;
        public function __construct(
            \Magento\Framework\App\ActionFactory $actionFactory,
            \Magento\Framework\App\ResponseInterface $response
        ) {
            $this->actionFactory = $actionFactory;
            $this->_response     = $response;

        }
        public function match(\Magento\Framework\App\RequestInterface $request)
        {

            $identifier  = trim($request->getPathInfo(), '/');
            $identifiers = explode('/', $$identifier);
            if (strpos($identifier, 'style') !== false && count($identifiers) == 3) {
                $request->setModuleName('style')->//module name
                    setControllerName('index')->//controller name
                    setActionName('index')->//action name
                    setParam($identifiers[1], $identifiers[2]); //custom parameters
            } else {
                return false;
            }
            return $this->actionFactory->create(
                'Magento\Framework\App\Action\Forward', ['request' => $request]
            );
        }

    }
  • are you need only one Url right? – Abdul Aug 28 '18 at 13:02
  • Yes i want to change only this one url – Sairaj Gadekar Aug 28 '18 at 13:22
0

By default, Magento have 3part in its URL.

http://example.com/{frontendname}/{ControllerName}/{Action}/

every Magento url must have this a pattern. If not internally, then url has frontend name and ControllerName,Action

http://myproject.com/style/index/index/parameter/paravalue.

Magento internally using url rewrite or custom Router, it is mapped URL with frontend Name, Controller name, and Action Name .

A good example is Catagory and product URL and Magento using URL rewrite.

The custom Router is CMS pages.

For your case, you have to build a custom Route. See how to build a custom router https://belvg.com/blog/how-to-create-custom-router-in-magento-2.html

Have change match function code.

 public function match(\Magento\Framework\App\RequestInterface $request) {
    $identifier = trim($request->getPathInfo(), '/');
    $identifiers = explode('/',$$identifier);
    if (strpos($identifier, 'style') !== false &&  count($identifiers) == 3) {
        $request->setModuleName('style')->//module name
                setControllerName('index')->//controller name
                setActionName('index')->//action name
                setParam($identifiers[1], $identifiers[2]); //custom parameters
    } else {
        return false;
    }
    return $this->actionFactory->create(
                    'Magento\Framework\App\Action\Forward', ['request' => $request]
    );
}
  • I created the custom Route and added the required code in di.xml .Also Executed the setup commands of upgrade n di compile n deploy but its not working . If i hit the url it shows url not found but old link is working. Also the match function from CustomStyle Controller is not being fired. – Sairaj Gadekar Aug 29 '18 at 12:21

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.