Magento 2.2.5:类工厂不存在

时间:2018-07-17 00:27:01

标签: module block factory adminhtml magento2.2

我正在上这门课程https://www.mage-world.com/blog/grid-and-form-in-magento-2-admin-panel-part-1.html,但是我的Magento版本是2.2.5,所以有点不同。我通过块而不是控制器自动创建了NewsFactory,并抛出了此错误消息

enter image description here

我在google和stackexchange上查找了5个小时,但仍然不知道为什么会出现此错误。所以这是我的代码:

我的封锁:C:\xampp\htdocs\magento\app\code\Fudu\HelloWorld\Block\Adminhtml\News.php

<?php

namespace Fudu\HelloWorld\Block\Adminhtml;

use \Magento\Framework\View\Element\Template;
use \Magento\Framework\View\Element\Template\Context;
use \Fudu\HelloWorld\Model\ResourceModel\Students\Collection as StudentsCollection;
use \Fudu\HelloWorld\Model\Students;
use \Fudu\HelloWorld\Model\NewsFactory;

class News extends Template
{
    /**
     * Constructor
     *
     */

    /**
     * News model factory
     * @var null|NewsFactory
     */
    protected $_newsFactory = null;

    /**
     * @param Context $context
     * @param NewsFactory $newsFactory
     * @param array $data
     */
    public function __construct(
        Context $context,
        NewsFactory $newsFactory,
        array $data = []
    )
    {
        $this->_newsFactory = $newsFactory;
        parent::__construct($context, $data);
        $this->_controller = 'adminhtml_news';
        $this->_blockGroup = 'Tutorial_SimpleNews';
        $this->_headerText = __('Manage News');
        $this->_addButtonLabel = __('Add News');
    }

    /**
     * @return Students[]
     */

    /** @var StudentsCollection $studentsCollection */
    public function execute()
    {
        $studentsCollection = $this->_newsFactory->create();
        $studentsCollection->addFieldToSelect('*')->load();
        return $studentsCollection->getItems();
    }

    /**
     * For a given students, returns its url
     * @param Students $students
     * @return string
     */
}

我的控制器:C:\xampp\htdocs\magento\app\code\Fudu\HelloWorld\Controller\Adminhtml\News.php

<?php

namespace Fudu\HelloWorld\Controller\Adminhtml;

use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\Registry;
use Magento\Framework\View\Result\PageFactory;

class News extends Action
{
    /**
     * Core registry
     *
     * @var \Magento\Framework\Registry
     */
    protected $_coreRegistry;

    /**
     * Result page factory
     *
     * @var \Magento\Framework\View\Result\PageFactory
     */
    protected $_resultPageFactory;

    /**
     * @param Context $context
     * @param Registry $coreRegistry
     * @param PageFactory $resultPageFactory
     */
    public function __construct(
        Context $context,
        Registry $coreRegistry,
        PageFactory $resultPageFactory
    )
    {
        parent::__construct($context);
        $this->_coreRegistry = $coreRegistry;
        $this->_resultPageFactory = $resultPageFactory;
    }

    /**
     * News access rights checking
     *
     * @return bool
     */


    public function execute()
    {
        $resultPage = $this->_resultPageFactory->create();
        return $resultPage;
    }
}

menu.xml(这是后端的菜单,当我们单击第三个选项“ Manage News”时,它将重定向到URL simplenews/news/index)。 C:\xampp\htdocs\magento\app\code\Fudu\HelloWorld\etc\adminhtml\menu.xml

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="../../../Backend/etc/menu.xsd">
    <menu>
        <add id="Fudu_HelloWorld::main_menu" title="Simple News"
             module="Fudu_HelloWorld" sortOrder="20"
             resource="Fudu_HelloWorld::simplenews" />

        <add id="Fudu_HelloWorld::add_news" title="Add News"
             module="Fudu_HelloWorld" sortOrder="1"
             parent="Fudu_HelloWorld::main_menu"
             action="simplenews/news/new"
             resource="Fudu_HelloWorld::manage_news" />

        <add id="Fudu_HelloWorld::manage_news" title="Manage News"
             module="Fudu_HelloWorld" sortOrder="2"
             parent="Fudu_HelloWorld::main_menu"
             action="simplenews/news/index"
             resource="Fudu_HelloWorld::manage_news" />

        <add id="Fudu_HelloWorld::configuration" title="Configurations"
             module="Fudu_HelloWorld" sortOrder="3"
             parent="Fudu_HelloWorld::main_menu"
             action="adminhtml/system_config/edit/section/simplenews"
             resource="Fudu_HelloWorld::configuration" />
    </menu>
</config>

我的观点:C:\xampp\htdocs\magento\app\code\Fudu\HelloWorld\view\adminhtml\layout\simplenews_news_index.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/
Framework/View/Layout/etc/page_configuration.xsd">
    <update handle="formkey"/>
    <update handle="simplenews_news_grid_block"/>
    <body>
        <referenceContainer name="content">
            <block class="Fudu\HelloWorld\Block\Adminhtml\News"
                   name="tutorial_simplenews_news.grid.container" />
        </referenceContainer>
    </body>
</page>

感谢您阅读本文,并祝您愉快。

1 个答案:

答案 0 :(得分:0)

运行此bin/magento setup:di:compile,很可能会给出一个错误,表明为什么无法创建此Factory