1

I'm a bit of a noob when it comes to module development for Magento 2, and I need some help: I've started developing a module (for testing purposes) on a localhost installation of Magento 2.3. Now, I think I've done everything right (I created the module.xml, the registration.php, ecc..) and I'm trying to output a simple message on the frontend. This is my code:

  1. routers.xml

    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <router id="standard">
        <route id="test" frontName="test">
            <module name="Ronk_Test" />
        </route>
    </router>
    

  2. test_index_index.xml

    <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <head>
        <title>Ronk Test Page</title>
    </head>
    <body>
        <referenceContainer name="content">
            <block class="Ronk\Test\Block\Test" name="testPage" template="Ronk_Test::test.phtml" />
        </referenceContainer>
    </body>
    

  3. test.phtml

 

echo $block->getTest();

echo __('This is a text.')

  1. Index.php
namespace Ronk\Test\Controller\Index;

use Magento\Framework\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;
use Magento\Framework\Controller\Result\ForwardFactory;
use Magento\Framework\Exception\NotFoundException;
use Magento\Framework\App\RequestInterface;

/**
 * Contact index controller
 */
class Index extends \Magento\Framework\App\Action\Action
{
    /**
     * @var PageFactory
     */
    protected $resultPageFactory;

    /**
     * @var ForwardFactory
     */
    protected $resultForwardFactory;

    /**
     * @var \Ronk\Test\Helper\Data
     */
    protected $helper;

    /**
     * Index constructor.
     *
     * @param \Magento\Framework\App\Action\Context $context
     * @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
     * @param \Magento\Framework\Controller\Result\ForwardFactory $resultForwardFactory
     * @param \Ronk\Test\Helper\Data $helper
     */
    public function __construct(
        Context $context,
        PageFactory $resultPageFactory,
        ForwardFactory $resultForwardFactory,
        \Ronk\Test\Helper\Data $helper
    ) {
        $this->resultPageFactory = $resultPageFactory;
        $this->resultForwardFactory = $resultForwardFactory;
        $this->helper = $helper;
        parent::__construct($context);
    }

    /**
     * Dispatch request
     *
     * @param RequestInterface $request
     * @return \Magento\Framework\App\ResponseInterface
     * @throws \Magento\Framework\Exception\NotFoundException
     */
    public function dispatch(RequestInterface $request)
    {
        if (!$this->helper->isEnabled()) {
            throw new NotFoundException(__('Page not found.'));
        }
        return parent::dispatch($request);
    }

    /**
     * Ronk Test Page
     *
     * @return \Magento\Framework\View\Result\Page
     */
    public function execute()
    {
        /** @var \Magento\Framework\View\Result\Page $resultPage */
        $resultPage = $this->resultPageFactory->create();
        $resultPage->getConfig()->getTitle()->set(__('Ronk Test Page'));
        if (!$resultPage) {
            $resultForward = $this->resultForwardFactory->create();
            return $resultForward->forward('noroute');
        }
        return $resultPage;
    }
} 
  1. Data.php
namespace Ronk\Test\Helper;

/**
 * Ronk Test helper
 */
class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
    /**
     * Path to store config if extension is enabled
     *
     * @var string
     */
    const XML_PATH_ENABLED = 'ronk/basic/enabled';

    /**
     * Check if extension enabled
     *
     * @return string|null
     */
    public function isEnabled()
    {
        return $this->scopeConfig->isSetFlag(
            self::XML_PATH_ENABLED,
            \Magento\Store\Model\ScopeInterface::SCOPE_STORE
        );
    }
}

I followed a guide, of course (being new to this) and I can't seem to understand what am I doing wrong. Whenever i go to 127.0.0.1/magento23/test/index/index it gives me 404. Any suggestions?

4
  • 1
    Did you enable your module?
    – Keyur Shah
    Dec 20, 2018 at 9:53
  • Yes, Everything is enabled Dec 20, 2018 at 9:56
  • 1
    could you please remove dispatch(RequestInterface $request) function from Ronk\Test\Controller\Index\ Index and check? i have suspect that it is returning false
    – Keyur Shah
    Dec 20, 2018 at 9:58
  • Tried, it still gives me 404 :( Dec 20, 2018 at 10:06

2 Answers 2

2

routes.xml file must be present in Ronk/Test/etc/frontend directory. Also make sure the file name is not routers.xml.

4
  • ...I'm an idiot... I called it routers. Sorry and thanks a lot, I would never had seen it :( Dec 20, 2018 at 10:53
  • Is it working now?
    – GenZ Dev
    Dec 20, 2018 at 10:58
  • Yes, it's working wonders now, thanks a lot! Dec 20, 2018 at 10:59
  • happy to know that ;-)
    – GenZ Dev
    Dec 20, 2018 at 11:54
0

Not entirely sure whether this is the problem but if you create a reference to your block like so:

$blockObj = $block->getLayout()->createBlock('Ronk\Test\Controller\Index\Index'); 

and then you can use $blockObjto access the block methods like $blockObj->getTest()

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.