0

Am crazy or what? Can somebody inspect my code and tell me what I do wrong?

I try examples from the official documentation:

https://devdocs.magento.com/guides/v2.4/extension-dev-guide/routing.html#router-class and getting 404 Not Found

Also I try to write some module like from here:

https://github.com/mageplaza/magento-2-sample-module

When I put this mageplaza module in my app/code directory all works fine. I see one difference between the Magepaza module router and router from the official documentation. Mageplaza does not use the Controller\Router class and does not use an extra configuration in app\fronted\di.xml for configuring custom routes. They just declare a new route with frontName and moduleName in fronted\routes.xml and everything works well.

I rewrote code from mageplaza module to my new module. All code is the some but still not working.

After all changes I clear cache, cache flushed, module:upgrade.

The module is enabled.

The module also exists in the setup_module database table.

Check my code

Module directory structure

│   composer.json
│   registration.php    
│
└───app
│   │      
│   └───Learing
│       │   
│       └───ModuleRoute  
│           │
│           └───Controller
│           │    │
│           │    └───Index    
│           │        │
│           │        └───Index.php  
│           └───etc
│               │
│               └───module.xml
│               │
│               └───fronted
│               │      │
│               │      └───routes.xml
│               │ 
│               └
│               view
│               │
│               └───fronted
│                   │
│                   └───layout 
│                       └───moduleroute_index_index.xml 

Learing/ModuleRoute/composer.json

{
  "name": "learing/moduleroute",
  "description": "",

  "type": "magento2-module",
  "version": "1.0.0",
  "license": "My License",
  "authors": [
    {
      "name": "Hi",
      "email": "[email protected]",
      "role": "Leader"
    }
  ],

  "autoload": {
    "files": [
      "registration.php"
    ],
    "psr-4": {
      "Learing\\ModuleRoute\\": ""
    }
  }

}

/Learing/ModuleRoute/registration.php

use Magento\Framework\Component\ComponentRegistrar;

ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Learing_ModuleRoute', __DIR__);

Learing/ModuleRoute/etc/module.xml

<?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="Learing_ModuleRoute" setup_version="0.0.1">
    </module>
</config>

Here i also try to remove setup_version but again all is the some!

Learing/ModuleRoute/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">

</config>

Learing/ModuleRoute/etc/fronted/routes.xml

<?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="moduleroute" frontName="moduleroute">
            <module name="Learing_ModuleRoute" />
        </route>
    </router>
</config>

Learing\ModuleRoute\Controller\Index\Index.php

<?php
namespace Learing\ModuleRoute\Controller\Index;

use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\View\Result\Page;
use Magento\Framework\View\Result\PageFactory;

class Index implements HttpGetActionInterface
{
    private $pageFactory;
    private $request;

    public function __construct(PageFactory $pageFactory, RequestInterface $request)
    {
        $this->pageFactory = $pageFactory;
        $this->request = $request;
   }
    public function execute()
    {
        return $this->_pageFactory->create();
    }
}

Learing/ModuleRoute/view/fronted/layout/moduleroute_index_index.xml

<?xml version="1.0"?>

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="page.main.title">
            <action method="setPageTitle">
                <argument translate="true" name="title" xsi:type="string">Routing Page</argument>
            </action>
        </referenceBlock>
    </body>
</page>

bin/magento setup:upgrade

bin/magento cache:flush

ivan@Ivans-MacBook-Pro project-community-edition % bin/magento module:status Learing_ModuleRoute
Learing_ModuleRoute: Module is enabled

I really don't know what is wrong here?? all write all some code just different namespace and names?

This also not working!

I try an example from doc

https://devdocs.magento.com/guides/v2.4/extension-dev-guide/routing.html#router-class

I create a new Controller/Router.php class and update fronted/di.xml and all is the some 404

Learing/ModuleRoute/Controller/Router.php

<?php
namespace Learing\ModuleRoute\Controller;

use Magento\Framework\App\Action\Forward;
use Magento\Framework\App\ActionFactory;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\App\RouterInterface;

class Router implements RouterInterface
{
    private $actionFactory;
    private $response;
 
    public function __construct(
        ActionFactory $actionFactory,
        ResponseInterface $response
    ) {
        $this->actionFactory = $actionFactory;
        $this->response = $response;
    }
 
    public function match(RequestInterface $request): ?ActionInterface
    {
        $identifier = trim($request->getPathInfo(), '/');

        if (strpos($identifier, 'moduleroute') !== false) {
            $request->setModuleName('Learing_ModuleRoute'); 
            // also try this $request->setModuleName('learing');
            $request->setControllerName('Index'); // also try lowercase
            $request->setActionName('index');

            return $this->actionFactory->create(Forward::class, ['request' => $request]);
        }
    }
}

Learing/ModuleRoute/etc/fronted/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="routingExample" xsi:type="array">
                        <item name="class" xsi:type="string">Learing\ModuleRoute\Controller\Router</item>
                        <item name="disable" xsi:type="boolean">false</item>
                        <item name="sortOrder" xsi:type="string">40</item>
                    </item>
                </argument>
            </arguments>
        </type>
    </config>

** 404 Not Found **

http://localhost/moduleroute/

http://localhost/moduleroute/index/

http://localhost/learing/moduleroute/index/index
1
  • 2
    TLTR. I see typo in "fronted" - should be "frontend" Dec 21, 2021 at 8:03

2 Answers 2

1

Could you please change your folder name fronted to frontend.

and run

php bin/magento cache:clean

4
  • 1
    Rejesh come and slap me in face please!
    – Ivan
    Dec 21, 2021 at 10:33
  • 1
    Yes coming ... hahaha Dec 21, 2021 at 10:48
  • I hate when is this happeing. I spend 5 hours to checking everything. Also i read files and directories names and i dont see mistake in one word. B.W thanks
    – Ivan
    Dec 21, 2021 at 10:57
  • sometime it's happend dude. glad It's working now Dec 21, 2021 at 11:10
0

Try to change code of controller as below

Learing\ModuleRoute\Controller\Index\Index.php

<?php

namespace Learing\ModuleRoute\Controller\Index;

use Magento\Framework\View\Result\PageFactory;

class Index extends \Magento\Framework\App\Action\Action
{
    /**
     * @var PageFactory
     */
    protected $resultPageFactory;
    
    /**
     * @param \Magento\Framework\App\Action\Context $context
     * @param PageFactory $resultPageFactory
     */
    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        PageFactory $resultPageFactory
    ) {
        $this->resultPageFactory = $resultPageFactory;
        parent::__construct($context);
    }
    
    /**
     * Default
     *
     * @return void
     */
    public function execute()
    {
        $this->_view->loadLayout();
        $this->_view->getLayout()->initMessages();
        //$this->_view->getPage()->getConfig()->getTitle()->set(__('Site Homebanner'));
         
        /** @var \Magento\Framework\View\Result\Page $resultPage */
        $resultPage = $this->resultPageFactory->create();
        return $resultPage;
    }
}
3
  • No this example give the some result. i already try that yesterday.
    – Ivan
    Dec 21, 2021 at 6:30
  • what kind of result? Dec 21, 2021 at 6:30
  • Result is 404. Like in my example.
    – Ivan
    Dec 21, 2021 at 7:56

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.