1

I'm trying to create a module which displays a CMS block with a custom route on which I can add some logic etc.

But I can't get the Module's controller to work.

The route I'm trying to create is www.example.com/wholesalers/info but when I go to that page url I get a 404 page.

I've followed this mageplaza tutorial but I can't get it to work.

I've been looking and looking but can't see what I'm missing...

Module Structure

app/code/Holy/
└── Wholesalers
    ├── composer.json
    ├── registration.php
    ├── Block
    │   └── Info.php
    ├── Controller
    │   └── Info
    │       └── Index.php
    ├── etc
    │   ├── module.xml
    │   └── routes.xml
    └── view
        └── frontend
            ├── layout
            │   └── wholesalers_info_index.xml
            └── templates
                └── wholesale-info.phtml

composer.json

{
    "name": "Holy/module-Wholesalers",
    "description": "",
    "minimum-stability": "dev",
    "require": {},
    "autoload": {
        "psr-4": {
            "Holy\\Wholesalers\\": ""
        },
        "files": [
            "registration.php"
        ]
    }
}

registration.php

<?php
/**
 * @category   Local
 * @package    Holy_Wholesalers
 */

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Holy_Wholesalers',
    __DIR__
);

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="Holy_Wholesalers" setup_version="1.0.0"/>
</config>

etc/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="wholesalers" frontName="wholesalers">
            <module name="Holy_Wholesalers" />
        </route>
    </router>
</config>

Controller/Info/Index.php

<?php
/**
 * @category   Local
 * @package    Holy_Wholesale
 */

namespace Holy\Wholesalers\Controller\Info;

class Index extends \Magento\Framework\App\Action\Action
{
    protected $_pageFactory;

    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $pageFactory)
    {
        $this->_pageFactory = $pageFactory;
        return parent::__construct($context);
    }

    public function execute()
    {
        return $this->_pageFactory->create();
    }
}

view/frontend/layout/wholesalers_info_index.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="2columns" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <referenceContainer name="content">
        <block class="Holy\Wholesalers\Block\Info" name="customer_wholesale_info" template="Holy_Wholesalers::wholesale-info.phtml" />
    </referenceContainer>
</page>

view/frontend/templates/wholesale-info.phtml

<h1>Hello World</h1>

And...

My Magento instance is in developer mode and I've cleared the cache etc

php bin/magento cache:flush
php bin/magento setup:upgrade
1
  • my phtml isn't working, is there anything else i need to add Commented Jun 3, 2020 at 16:04

2 Answers 2

4

Try moving your routes.xml to etc/frontend/routes.xml

cache:clean + setup:upgrade + setup:di:compile
1
  • 1
    you faster than me, upvote for you ;) Commented Jan 25, 2018 at 16:27
2

Try moving etc/routes.xml file to etc/frontend/routes.xml

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.