1

When run the custom module getting 404 error. Anyone, please help me.

app/code/Tychons/Registration/Block/Index.php

<?php
/**
 * Copyright © 2013-2017 Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

namespace Tychons\Registration\Block;
use Magento\Framework\View\Element\Template\Context;
use Magento\Framework\ObjectManagerInterface;

class Index extends \Magento\Framework\View\Element\Template
{
    protected $customerSession;
    protected $storeManager;
    protected $_object;
    /**
 * @var \Magento\Framework\ObjectManagerInterface
 */

    public function __construct(Context $context,ObjectManagerInterface $interface,array $data = []){
        $this->_object = $interface;
        //$this->storeManager = $storeManager;
        $this->storeManager = $context->getStoreManager();
        parent::__construct($context, $data);
    }


    public function getBaseUrl()
    {
        return $this->storeManager->getStore()->getBaseUrl();
    }



}

app/code/Tychons/Registration/Controller/Verification.php

<?php
namespace Tychons\Registration\Controller;
use Magento\Framework\Controller\ResultFactory; 
class Verification extends \Magento\Framework\App\Action\Action {
    public function execute() {
            $resultPage = $this->resultFactory->create(ResultFactory::TYPE_PAGE);       
            $resultPage->getConfig()->getTitle()->set(__('Email Verification'));
            return $resultPage;
    }

}

app/code/Tychons/Registration/etc/frontend/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="verification" frontName="verification"> 
            <module name="Tychons_Registration" /> 
        </route> 
    </router> 
</config>

app/code/Tychons/Registration/view/frontend/layout/registration_verification.xml

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">

    <body>
        <referenceBlock name="page.main.title" as="top"  remove="true">
            <action method="setPageTitle">
                <argument translate="true" name="title" xsi:type="string">Verification</argument>
            </action>
        </referenceBlock>
        <referenceContainer name="content">
             <block class="Tychons\Registration\Block\Index" name="verification" template="Tychons_Registration::verification.phtml" after='page.main.title' cacheable="false">
             </block>
        </referenceContainer>
    </body>
</page>

app/code/Tychons/Registration/view/frontend/templates/verification.phtml

<?php echo "verificatiob page"; ?>

app/code/Tychons/Registration/registration.php

<?php
/**
 * Module configuration
 */
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Tychons_Registation',
    __DIR__
);

app/code/Tychons/Registration/etc/module.xml

 <?xml version="1.0"?>
    <!--
    /**
     * Module configuration.
     */
    -->
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
            <module name="Tychons_Registration" setup_version="1.0.0" />
    </config>
6
  • Do you declare module.xml and registration.php files? Commented Apr 4, 2018 at 7:11
  • yes module is enabled Commented Apr 4, 2018 at 7:11
  • changed name to verification_index_index.xml and the run the url like url/verification but getting 404 error Commented Apr 4, 2018 at 7:44
  • Your xml name is verification_index_index.xml. So you have to change controller path as app/code/Tychons/Registration/Controller/Index/Index.php Then cache clean and check. Commented Apr 4, 2018 at 8:36
  • Your Tychons/Registration/view/frontend/layout/registration_verification.xml and app/code/Tychons/Registration/Controller/Verification.php are wrong. Please read my answer careful. Commented Apr 4, 2018 at 9:15

4 Answers 4

0

Your controller should be like this:

app/code/Tychons/Registration/Controller/Verifications/Index.php

instead of

app/code/Tychons/Registration/Controller/Verification.php

Now you call your module like this:

www.domaine.com/verification/verifications

Dont forget to run the setup upgrade like this: php bin/magento setup:upgrade

0

Change your controller path to

Tychons/Registration/Controller/Verification/Index.php

<?php

namespace Tychons\Registration\Controller\Verification;

use Magento\Framework\View\Result\PageFactory;

class Index extends \Magento\Framework\App\Action\Action
{
    public function __construct(
        Context $context,
        PageFactory $resultPageFactory
    ) {
        parent::__construct($context);
        $this->resultPageFactory = $resultPageFactory;
    }

    public function execute() {
        $resultPage = $this->resultPageFactory->create();
        $resultPage->getConfig()->getTitle()->set(__('Email Verification'));
        return $resultPage;
    }
}

Now clean the cache and remove var/generation folder if your magento version is lower than 2.2.x otherwise remove generated folder.

Now run the url as your_magento_url/verification/verification/index

0

First of all, take a look here

vendor/magento/module-store/etc/frontend/di.xml

<type name="Magento\Framework\App\RouterList" shared="true">
        <arguments>
            <argument name="routerList" xsi:type="array">
                <item name="standard" xsi:type="array">
                    <item name="class" xsi:type="string">Magento\Framework\App\Router\Base</item>
                    <item name="disable" xsi:type="boolean">false</item>
                    <item name="sortOrder" xsi:type="string">30</item>
                </item>
                .....
            </argument>
        </arguments>
    </type>

The base router will use this class: \Magento\Framework\App\Router\Base

public function match(\Magento\Framework\App\RequestInterface $request)
{
    $params = $this->parseRequest($request);

    return $this->matchAction($request, $params);
}




protected function parseRequest(\Magento\Framework\App\RequestInterface $request)
{
    ......
    foreach ($this->_requiredParams as $paramName) {
        $output[$paramName] = array_shift($params);
    }
    ......
}

protected $_requiredParams = ['moduleFrontName', 'actionPath', 'actionName'];

As we can see, we need 3 params, but only pass only 2 params (Your url: verification/verification). So, the actionName will be null. So the default action name will be index. But, we don't have index action too. So, 404 not found will be loaded.

Back to your code, I saw:

Your layout should be frontMame_actionPath_actionName.xml. frontName is from etc/frontend/routes.xml.

So, we need to create actionName, your controller should be:

Tychons/Registration/Controller/Verification/Index.php

<?php

namespace Tychons\Registration\Controller\Verification;

use Magento\Framework\Controller\ResultFactory;


class Index extends \Magento\Framework\App\Action\Action
{

    public function execute() {
        $resultPage = $this->resultFactory->create(ResultFactory::TYPE_PAGE);
        $resultPage->getConfig()->getTitle()->set(__('Email Verification'));
        return $resultPage;
    }
}

Tychons/Registration/view/frontend/layout/verification_verification_index_xml => frontname_actionPath_actionName.xml

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">

    <body>
        <referenceContainer name="content">
            <block class="Tychons\Registration\Block\Index" name="verification" template="Tychons_Registration::verification.phtml" after='page.main.title' cacheable="false">
            </block>
        </referenceContainer>
    </body>
</page>

Now, input url: verification/verification/index or verification/verification. We will see the result.

7
  • modified the files but still same 404 error Commented Apr 4, 2018 at 8:04
  • Try to clear Magento Cache. If still not working, please update your question with the latest code. Commented Apr 4, 2018 at 8:05
  • cleared the cache Commented Apr 4, 2018 at 8:07
  • Please, update your question with the latest code. Including module.xml and registration.php files. Commented Apr 4, 2018 at 8:09
  • added the module.xml and registration.php files please check. Commented Apr 4, 2018 at 9:02
0

Create directory Verification in Controller folder and copy paste the controller into it, and rename it to Index.php, also change class name inside.

Change layout xml filename to verification_verification_index.xml

run cli commands: php bin/magento c:c php bin/magento s:d:c

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.