I working on creating custom module in magento 2, but it's not working. Could you please suggest me where i went wrong?.
My module code:
app/etc/config.php:
'modules' =>
array (
'Ramesh_Sample' => 1,
),
app/code/Ramesh/Sample/etc/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="Ramesh_Sample" schema_version="2.0.0">
</module>
</config>
app/code/Ramesh/Sample/etc/frontend/routes.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/App/etc/routes.xsd">
<router id="standard">
<route id="sample" frontName="sample">
<module name="Ramesh_Sample" />
</route>
</router>
</config>
app/code/Ramesh/Sample/Controller/Index/Index.php
<?php
namespace Ramesh\Sample\Controller\Index;
use Magento\Framework\App\Action\Context;
//use \Magento\Framework\View\Result\PageFactory;
class Index extends \Magento\Framework\App\Action\Action
{
private $_resultPageFactory;
public function __construct(
Context $context,
\Magento\Framework\View\Result\PageFactory $resultPageFactory
)
{
parent::__construct($context);
$this->_resultPageFactory = $resultPageFactory;
}
public function execute()
{
$result = $this->_resultPageFactory->create();
return $result;
}
}
app/code/Ramesh/Sample/Block/Sample.php
namespace Magento\Sample\Block;
class Sample extends \Magento\Framework\View\Element\Template
{
}
app/code/Ramesh/Sample/views/frontend/layout/sample_index_index.xml
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
<head>
<title>Hello World</title>
</head>
<body>
<referenceContainer name="content">
<block class="Sample\Ramesh\Block\Sample" name="sample" template="sample.phtml">
</block>
</referenceContainer>
</body>
</page>
app/code/Ramesh/Sample/view/frontend/templates/sample.phtml
<?php echo 'Hi I am Magento 2'; ?>
But I got the following error:
a:4:{i:0;s:380:"Recoverable Error: Argument 2 passed to Ramesh\Sample\Controller\Index\Index::__construct() must be an instance of Magento\Framework\View\Result\PageFactory, none given, called in var/generation/Ramesh/Sample/Controller/Index/Index/Interceptor.php on line 14 and defined in app/code/Ramesh/Sample/Controller/Index/Index.php on line 14";i:1;s:6109:"#0 app/code/Ramesh/Sample/Controller/Index/Index.php(14): Magento\Framework\App\ErrorHandler->handler(4096, 'Argument 2 pass...', '/var/www/html/m...', 14, Array)
-
See below link How to create Custom Module in Magento 2? magento.stackexchange.com/questions/54609/…Manoj Kumar– Manoj Kumar2015年08月27日 06:33:22 +00:00Commented Aug 27, 2015 at 6:33
3 Answers 3
try to delete your module from the var/generation/vendor-name/your-module and try to refresh the page and check if it's working.
-
Why do I have to delete the complete var/generation folder? Is there a faster process? Do I have to run bin/magento setup:di:compile every time?tester– tester2016年02月17日 20:49:48 +00:00Commented Feb 17, 2016 at 20:49
-
1You don't need to delete the complete var/generation folder try to identify your correct folder of your module Example : Demo /Module inside the var/generation .The code bin/magento setup:di:compile works only if you set the dependencies or code generation patterns set in di.xmlNiranjan B– Niranjan B2016年02月18日 04:45:20 +00:00Commented Feb 18, 2016 at 4:45
-
This has happened to me while working with controllers and I fixed it the same way, but can somebody explain why this happens in the first place?diazwatson– diazwatson2017年03月05日 23:52:41 +00:00Commented Mar 5, 2017 at 23:52
-
tried the same above code and deleted files as mentioned but still showing error - HTTP ERROR 500,Amit Dwivedi– Amit Dwivedi2019年07月27日 06:34:39 +00:00Commented Jul 27, 2019 at 6:34
You can use below mentioned command and then refresh the page.
php bin/magento setup:upgrade
-
-
Great ;) Working for meAbdulBasit– AbdulBasit2017年10月18日 07:40:18 +00:00Commented Oct 18, 2017 at 7:40
Replace code given below in your constructor
$this->_pageFactory = $pageFactory;
return parent::__construct($context);
Explore related questions
See similar questions with these tags.