Add a Model to Joomla Admin Part
The HtmlView class asks the model for the data using the get method of the BaseHtmlView class.
For example, the get method converts the get('Msg') call into a getMsg() call on the model, which is the function which you have to provide in the model.
The model files are stored in the folder: src/Model.
Earlier, we have added the title in the default.php layout file directly. Now, the view file will get this title from the model and then, the layout file will display the title.
Step 1: Create Model File
src/Model/PlanetsModel.php
namespace TechFry\Component\Planets\Administrator\Model;
defined('_JEXEC') or die;
use Joomla\CMS\MVC\Model\ListModel;
class PlanetsModel extends ListModel
{
public function getTitle()
{
return 'Welcome to My Planet!';
}
}
We are extending the ListModel as later on this will display the list of planets on the backend.
Step 2: Edit View File
src/View/Planets/HtmlView.php
Now, this view file will get the title from the model method we have created in the first step.
class HtmlView extends BaseHtmlView
{
public $title;
public function display($tpl = null)
{
$this->title = $this->get('Title');
parent::display($tpl);
}
}
Step 3: Edit Layout File
tmpl/planets/default.php
<h2><?php echo $this->title; ?></h2>
The layout file will display the title from the second step.