I know that there are similar questions to this but none of the other questions fix my problem.
I'm still doing Magento training and I'm practicing to create a frontend controller that renders "HELLO WORLD" (module name: Unit2_HelloWorldController)
What I have created:
- Declare a route in Unit2/HelloWorldController/etc/frontend/routes.xml
<?xml version="1.0"?>
<!--
/**
* Copyright © 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="helloworldcontroller" frontName="helloworldcontroller">
<module name="Unit2_HelloWorldController" />
</route>
</router>
</config>
- Create an action class in HelloWorldController/Controller/index.php
<?php
/**
*
* Copyright © Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Unit2\HelloWorldController\Controller\Action;
/**
* Class Index
* @package Unit2\HelloWorldController\Controller\Action
*/
class Index extends \Magento\Framework\App\Action\Action {
/**
* @var \Magento\Framework\View\Result\PageFactory
*/
protected $_pageFactory;
/**
* Index constructor.
* @param \Magento\Framework\App\Action\Context $context
* @param \Magento\Framework\View\Result\PageFactory $pageFactory
*/
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Framework\View\Result\PageFactory $pageFactory
)
{
$this->_pageFactory = $pageFactory;
return parent::__construct($context);
}
/**
* @return \Magento\Framework\Controller\ResultInterface
*/public function execute()
{
$result = $this->resultFactory->create(\Magento\Framework\Controller\ResultFactory::TYPE_RAW);
$result->setContents('Hello World');
return $result;
}
}
- Create HelloWorldController/registration.php
<?php
/**
*
* Copyright © Magento. All rights reserved. * See COPYING.txt for license details.
*/
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Unit2_HelloWorldController',
__DIR__
);
- Create HelloWorldController/etc/module.xml
<?xml version="1.0"?>
<!--/** * * Copyright © 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="Unit2_HelloWorldController" setup_version="0.0.1"></module>
</config>
Then I ran these commands
php bin/magento setup:upgrade
php bin/magento setup:static-content:deploy -f
and when I try to open http://localhost:8080/magento2/helloworldcontroller/action/index I got 404 Not Found.
What am I doing wrong here? Any helps would be appreciated. Thank you.