-
Notifications
You must be signed in to change notification settings - Fork 30
004. Adding a model to the site part
Now, we want to create a view on the front end.
Newly created files
components/com_foos/Model/FooModel.php
Modified files
administrator/components/com_foos/foos.xml
components/com_foos/View/Foo/HtmlView.php
components/com_foos/tmpl/foo/default.php
https://github.com/astridx/boilerplate/compare/t3...t4
components/com_foos/Model/FooModel.php
To create the model, create another file named FooModel.php
under components/com_foos/Model/
and the the following code.
Joomla! use a model to read and write data. In our first example, this is very simple. It can get more complicated later. Data can come from different sources and sometimes have to be prepared.
Let us start by creating a simple model for the front end view.
First we write the necessary things.
<?php
/**
* @package Joomla.Site
* @subpackage com_foos
*
* @copyright Copyright (C) 2005 - 2019 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\Component\Foos\Site\Model;
defined('_JEXEC') or die;
use Joomla\CMS\MVC\Model\BaseDatabaseModel;
Then we create the class in the form (name of the component)+Model
, in our case FooModel
, which extends from the base BaseDatabaseModel
class.
/**
* Foo model for the Joomla Foos component.
*
* @since 4.0.0
*/
class FooModel extends BaseDatabaseModel
{
/**
* @var string message
*/
protected $message;
/**
``` php
The first function in our class creates a `message`.
For this, it checks if the `message` is set.
If the `message` is not set, it set it to a default value `'Hello Foo!'`.
In the end it it returns this `message`.
We will use this function later in the view. A simple call `$this->msg = $this->get('Msg');` in the view is then sufficient to display content.
``` * Get the message
*
* @return string The message to be displayed to the user
*/
public function getMsg()
{
if (!isset($this->message))
{
$this->message = 'Hello Foo!';
}
return $this->message;
}
}
We need to modify our installation XML file, the update server and the change log to include the new version.
administrator/components/com_foos/foos.xml
$this->msg = $this->get('Msg');
components/com_foos/View/Foo/HtmlView.php
components/com_foos/tmpl/foo/default.php
Omitting the closing tag at the end of a file is just one solution for avoiding blanks and other characters at the end of file. For example any char which is accidentally added behind the closing tag would trigger an error when trying to modify header info later.
Removing the closing tag is kind of "good practice" referring to many coding guidelines.
It is important to know: Every time you press return
on your keyboard you're actually inserting an invisible character
called a line ending.
Historically, different operating systems have handled line endings differently.
Because utilities that are supposed to operate on files may not cope well with lines that don't end with a newline. Sometimes, lines not ending in a newline character aren't considered actual lines. Which means: if your last line is a line of code, it might be ignored by those tools.
An argument can also be made for cleaner diffs.
Changing:
...
defined('_JEXEC') or die;
?>
Hello Foos (invisible character)
to:
...
defined('_JEXEC') or die;
?>
Hello Foos
(invisible character)
involves only a one-line change in the diff:
...
defined('_JEXEC') or die;
?>
Hello Foos
+ <?php macheEtwas()
(invisible character)
This beats the more confusing multi-line diff when the trailing comma was omitted:
...
defined('_JEXEC') or die;
?>
- Hello Foos (invisible character)
+ Hello Foos
+ <?php macheEtwas()(invisible character)