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:
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>
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>
test.phtml
echo $block->getTest();
echo __('This is a text.')
- 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; } }
- 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?
dispatch(RequestInterface $request)
function fromRonk\Test\Controller\Index\ Index
and check? i have suspect that it is returning false