9

I have created 2 custom module inside vendor folder .This is my layout structure :

-app
  -code
    -Company
      -Blog
      -HelloWorld
        -Controller
          -Hello
            -World.php
        -registration.php
        -etc
          -module.xml
          -frontend
            -routes.xml

Both are enabled .

This is my code for routes.xml for HelloWorld module :

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

This is my code in module.xml :

 <?xml version="1.0"?>
   <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="Company_HelloWorld" setup_version="1.0.0">
   </module>
   </config>

This is my registration.php file :

<?php 
   \Magento\Framework\Component\ComponentRegistrar::register(
   \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Company_HelloWorld',
    __DIR__
);

This is my controller code :

 <?php
 namespace Company\HelloWorld\Controller\Hello;


 class World extends \Magento\Framework\App\Action\Action
 {
   public function __construct(
    \Magento\Framework\App\Action\Context $context)
  {
      return parent::__construct($context);
  }

  public function execute()
  {
    echo 'Hello World';
    exit;
  } 
} 

This is my layout code :

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="2columns-left" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>

</body>
</page>

& Same for another module. I have already executed setup:upgrade command but still, I am getting 404 error when I try to access any one these modules.

6
  • can you show your layout file and tree folder structure of your module Commented Jul 6, 2016 at 6:23
  • I have updated my question.Currently I have given exit; in execute() method of controller but it does not even come in controller I guess. Commented Jul 6, 2016 at 6:31
  • In magento2 You can create module Inside app/code folder
    – MaYaNk
    Commented Jul 6, 2016 at 6:31
  • I have created inside app/code only . Commented Jul 6, 2016 at 6:32
  • i can't perfectly understand your layout structure.
    – MaYaNk
    Commented Jul 6, 2016 at 6:49

6 Answers 6

5

Try this 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="Company_HelloWorld" setup_version="1.0.0" active="true">

3
  • still the same error. Commented Jul 6, 2016 at 6:36
  • I edited my answer
    – MaYaNk
    Commented Jul 6, 2016 at 7:05
  • try to change setup_version="1.0.0" to setup_version="2.0.1"
    – MaYaNk
    Commented Jul 6, 2016 at 7:07
5

I have attach one Demo Tutorial for you.

Here is the link https://drive.google.com/file/d/0B7-ErEahwIPYc0xEOVh3eWV1ejA/view?usp=sharing

Put it inside your app/code folder.
please check it. hope this will help you.

1
  • After add _isAllowed function on Controller url works protected function _isAllowed() { return true; } Commented Aug 12, 2021 at 9:29
1

you have no created any controller so it will not find any url so it will give you 404 error

Create Controller folder

Create Index folder inside Controller folder

Create Index.php file inside Index folder add below code in it.

<?php

namespace  Company\HelloWorld\Controller\Index;
use Magento\Framework\View\Result\PageFactory;
use Magento\Framework\App\Action\Context;
class Index extends \Magento\Framework\App\Action\Action
{
    protected $resultPageFactory;
     public function __construct(
        Context $context,
        PageFactory $resultPageFactory
    ) {
        $this->resultPageFactory = $resultPageFactory;
        parent::__construct($context);
    }
    /**
     * Index action
     *
     * @return $this
     */
    public function execute()
    {
        echo "helloworld";exit;
    }   



}

Now you can check url helloworld/index/index

5
  • Ofcourse I have created controller .I just didn't paste it's code in question Commented Jul 6, 2016 at 7:01
  • I have added controller code in my question. Commented Jul 6, 2016 at 7:07
  • Which url are you trying? Commented Jul 6, 2016 at 7:42
  • Now it comes to execute() method of controller but displays only blank page.localhost/magento-demo/helloworld/hello/world Commented Jul 6, 2016 at 9:13
  • It worked after I executed cache flush command. Commented Jul 6, 2016 at 9:40
0

You need to change code in 2 files,

app/code/Company/HelloWorld/etc/frontend/routes.xml

<?xml version="1.0"?>
<!--
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
 <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
  <router id="standard">
    <route id="company_helloworld" frontName="helloworld">
        <module name="Company_HelloWorld"/>
    </route>
 </router>
</config>

app/code/Company/HelloWorld/etc/module.xml

<?xml version="1.0"?>
<!--
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Company_HelloWorld" setup_version="2.1.1">
   </module>
   </config>

I have just made this two changes run php bin/magento setup:upgrade and its working fine.

localhost/magento-demo/index.php/helloworld/hello/world/

enter image description here

0

I executed bin\magento cache:flush & It worked apparently.It was a cache issue.

0

Just try to Deploy the content and upgrade the setup

php bin/magento setup:upgrade

php bin/magento setup:static-content:deploy

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.