0

I have created Custom Module. As it requires some front-end routing, have to create Router.php

Whenever I'm accessing Magento eCommece Frontend other pages, say Category/Product Page. It will call my custom module's Router.php

So how can I limit that this Router.php, so it will be called only when I access my Custom Module (App\Code\Custom\Module)

In Addition, Due to this Router.php, my other custom module pages are not working, which is already working fine before.

I'm using this module as a reference for creating my custom module. https://github.com/tzyganu/Magento2SampleModule/tree/master/Controller

Due to this, my module's other pages are not working. Help needed Urgently.

[UPDATE]

Have issue with app\code\Alphanova\ETrade\etc\frontend\routes.xml

<?xml version="1.0"?>
      <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/App/etc/routes.xsd">
      <router id="standard">
          <route id="mymodule" frontName="mymodule">
              <module name="Custom_Module" />
           </route>
      </router>
</config>

So other pages are working fine now. But now detail page is not working.

http://localhost/magento2/mymodule => Working

http://localhost/magento2/mymodule/test.html => Not Working.

Previous it's working when i have <route id="custom_module" frontName="custom_module"> in routes.xml

app\code\Custom\Module\etc\frontend\di.xml

  <?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
    <type name="Magento\Framework\App\RouterList">
        <arguments>
            <argument name="routerList" xsi:type="array">
                <item name="Custom_Module" xsi:type="array">
                    <item name="class" xsi:type="string">Custom\Module\Controller\Router</item>
                    <item name="disable" xsi:type="boolean">false</item>
                    <item name="sortOrder" xsi:type="string">22</item>
                </item>
            </argument>
        </arguments>
    </type>
    <virtualType name="CustomModuleRoutingEntityPosts" type="Custom\Module\Model\Routing\Entity">
        <arguments>
            <argument name="prefixConfigPath" xsi:type="const">Custom\Module\Model\Posts\Url::URL_PREFIX_CONFIG_PATH</argument>
            <argument name="suffixConfigPath" xsi:type="const">Custom\Module\Model\Posts\Url::URL_SUFFIX_CONFIG_PATH</argument>
            <argument name="factory" xsi:type="object">Custom\Module\Model\PostsFactory</argument>
            <argument name="controller" xsi:type="string">posts</argument>
        </arguments>
    </virtualType>
    <type name="Custom\Module\Controller\Router">
        <arguments>
            <argument name="routingEntities" xsi:type="array">
                <item name="posts" xsi:type="object">CustomModuleRoutingEntityPosts</item>
            </argument>
        </arguments>
    </type>
</config>

app\code\Custom\Module\Controller\Posts\View.php

  public function execute() {
    echo "1111<pre>";
    exit;
  }

It's not going to controller & gives 404 page error.

[Solved]

It's issue with $request->setModuleName('mymodule'); in Router.php, previously it's $request->setModuleName('custom_module');

Still question is why Product/Catalog routes go to my custom Router.php? How can i set that Router.php file only called for some Module & Controller otherwise not. Because we have configuration in di.xml so that's why it's calling

0

You only need a custom router.php file if you plan to use custom urls. I mean something else than module/controller/action.
If you don't need urls like mymodule/test.html then you don't need that router.php file and you don't need this in etc/frontend/di.xml

<type name="Magento\Framework\App\RouterList">
    <arguments>
        <argument name="routerList" xsi:type="array">
            <item name="sample_news" xsi:type="array">
                <item name="class" xsi:type="string">Namespace\Module\Controller\Router</item>
                <item name="disable" xsi:type="boolean">false</item>
                <item name="sortOrder" xsi:type="string">10</item>
            </item>
        </argument>
    </arguments>
</type>

If you want url's like mymodule/test.html you need that router file.
And it is normal to have it called when you view the product pages.
Magento accepts a set of router classes. The method match is called from each of these classes until a route is matched to something.

  • Expecting answer from you @Marius. app\code\Custom\Module\Controller\Router.php @ End of file when i do $this->dispatched = true; echo 1;exit;. It works fine. But when i remove exit & @ last line return $this->actionFactory->create(Forward::class); it goes to 404 page. What exactly i need to do?. I have installed ur module it works fine. Issue is with my custom module which results 404 page. – Ankit Shah Sep 15 '16 at 7:33
  • Maybe the current url you are on is not matched to anything and you get a 404 page as you should. – Marius Sep 15 '16 at 7:35
  • I have added more info in question. Page Identifier / URL Key exists in database. Will get value from DB using checkUrlKey function. – Ankit Shah Sep 15 '16 at 7:57

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.