I'm trying to get data from my custom table and show it to frontend.
I followed to this tutorial: https://www.emiprotechnologies.com/technical_notes/magento-technical-notes-60/post/how-to-create-model-resource-model-and-collection-in-magento-2-400
Here is my code:
app/code/HaoNMN/Museinc/Model/SampleTable.php:
<?php
namespace HaoNMN\Museinc\Model;
use Magento\Framework\Model\AbstractModel;
class SampleTable extends AbstractModel
{
/**
* Define resource model
*/
protected function _construct()
{
$this->_init('HaoNMN\Museinc\Model\ResourceModel\SampleTable');
}
}
app/code/HaoNMN/Museinc/Model/ResourseModel/SampleTable.php:
<?php
namespace HaoNMN\Museinc\Model\ResourceModel;
use Magento\Framework\Model\ResourceModel\Db\AbstractDb;
class SampleTable extends AbstractDb
{
/**
* Define main table
*/
protected function _construct()
{
$this->_init('museinc_sampletable', 'id');
}
}
app/code/HaoNMN/Museinc/Model/ResourseModel/SampleTable/Collection.php:
<?php
namespace HaoNMN\Museinc\Model\ResourceModel\SampleTable;
use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
class Collection extends AbstractCollection
{
/**
* Define model & resource model
*/
protected function _construct()
{
$this->_init(
'HaoNMN\Museinc\Model\SampleTable',
'HaoNMN\Museinc\Model\ResourceModel\SampleTable'
);
}
}
app/code/HaoNMN/Museinc/Controller/Index/Index.php:
<?php
namespace HaoNMN\Museinc\Controller\Index;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use HaoNMN\Museinc\Model\SampleFactory;
class Index extends \Magento\Framework\App\Action\Action
{
protected $_modelFactory;
/**
* @param Context $context
* @param NewsFactory $modelNewsFactory
*/
public function __construct(
Context $context,
SampleFactory $modelFactory
) {
parent::__construct($context);
$this->_modelFactory = $modelFactory;
}
public function execute()
{
$sampleModel = $this->_modelFactory->create();
$item = $sampleModel->load(1);
var_dump($item->getData());
$data = $sampleModel->getCollection();
var_dump($data->getData());
}
}
And final is my route:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
<router id="standard">
<route id="museinc" frontName="museinc">
<module name="HaoNMN_Museinc"/>
</route>
</router>
</config>
But when I access my url: http://localhost/magentoCE233/museinc/index/index, it got error like this:
I search some post in here and someone said that this is solved by run command php bin/magento setup:di:compile. But when I run, it showed this error:
Can you tell me what wrong did I do?
ResourseModel
in the file names you actually meanResourceModel
?use HaoNMN\Museinc\Model\SampleTableFactory;
in your controller and check it.ResourceModel
folder name. You write in question isResourseModel
.