在magento2中创建自定义模块显示致命错误


9

我正在magento 2中创建自定义模块,但无法正常工作。你能建议我哪里出问题了吗?

我的模块代码:

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>

应用程序/代码/ Ramesh /示例/控制器/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;
    }
}

应用程序/代码/ Ramesh /示例/块/Sample.php

namespace Magento\Sample\Block;
class Sample extends \Magento\Framework\View\Element\Template
{
}

应用程序/代码/ Ramesh /样本/视图/前端/布局/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'; ?>

但我收到以下错误:

a:4:{i:0; s:380:“可恢复的错误:传递给Ramesh \ Sample \ Controller \ Index \ Index :: __ construct()的参数2必须是Magento \ Framework \ View \ Result \ PageFactory的实例,没有给出,在第14行的var / generation / Ramesh / Sample / Controller / Index / Index / Interceptor.php中调用,并在第14行的app / code / Ramesh / Sample / Controller / Index / Index.php中定义“; i: 1; s:6109:“#0应用程序/代码/ Ramesh /示例/控制器/Index/Index.php(14):Magento\Framework\App\ErrorHandler->handler(4096,“参数2通过...”, '/var/www/html/m...',14,数组)


参见下面的链接如何在Magento 2中创建自定义模块? magento.stackexchange.com/questions/54609/...
马诺·库马尔·

Answers:


29

尝试从var / generation / vendor-name / your-module中删除您的模块,并尝试刷新页面并检查其是否正常工作。


为什么必须删除完整的var / generation文件夹?有更快的流程吗?每次都必须运行bin / magento setup:di:compile吗?
测试仪

1
您无需删除完整的var / generation文件夹即可尝试识别模块的正确文件夹示例:var / generation内的Demo / Module。仅当您设置依赖项时,bin / magento setup:di:compile代码才有效或在di.xml中设置的代码生成模式
Niranjan B

这是我在使用控制器时发生的,我以相同的方式解决了这个问题,但是有人可以解释一下为什么会首先发生这种情况吗?
diazwatson

尝试了相同的上述代码并删除了提到的文件,但仍显示错误-HTTP错误500,
amit_game


By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.