Class Factory doesn't exists
-
18-01-2021 - |
Question
I am after this course https://www.mage-world.com/blog/grid-and-form-in-magento-2-admin-panel-part-1.html but my magento version is 2.2.5 so its a little bit different. i create NewsFactory auto by block instead of controller. and its throw this err message:
i'm looked on google and stackexchange for 5 hours, but still dont know why this error appear.
so here is my code:
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
*/
}
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 (this is a menu from backend, when we click on the 3rd option, which is "Manage News" , it will redirect to url simplenews/news/index
).
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>
Solution 2
Ah i found the answer, it because i need to create a Model after the name of Factory ex: if u want to create NewsFactory, u will need to create News Model
OTHER TIPS
If you notice in the screenshot you shared. There is one more News folder under adminhtml. You didn't notice that. So, change
namespace Fudu\HelloWorld\Block\Adminhtml
to
namespace Fudu\HelloWorld\Block\Adminhtml\News
Please make sure to clear generated folder and clear the cache.
Also in the same block, there are slashes before class path in 'use' statements. Please remove them.
Change
use \Magento\Framework\View\Element\Template;
To
use Magento\Framework\View\Element\Template;