Skip to content
This repository has been archived by the owner on Nov 5, 2022. It is now read-only.

004. Adding a model to the site part

Astrid edited this page Jan 4, 2020 · 16 revisions

Adding a model to the site part

In this chapter we will ...

Now, we want to create a view on the front end.

t4_1

Newly created or Modified files

Newly created files

components/com_foos/Model/FooModel.php

Modified files

changelog.xml

foo_update.xml

administrator/components/com_foos/foos.xml

components/com_foos/View/Foo/HtmlView.php

components/com_foos/tmpl/foo/default.php

All changes at a glance

https://github.com/astridx/boilerplate/compare/t3...t4

Detailed explanations

File Structure

Newly created files

components/com_foos/Model/FooModel.php

<?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;
/**
 * Foo model for the Joomla Foos component.
 *
 * @since  1.0.0
 */
class FooModel extends BaseDatabaseModel
{
	/**
	 * @var string message
	 */
	protected $message;
	/**
	 * 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;
	}
}

Modified files

We need to modify our installation XML file, the update server and the change log to include the new version.

changelog.xml

Comparing t3 t4 · astridx boilerplate(5)

foo_update.xml

Comparing t3 t4 · astridx boilerplate(3)

administrator/components/com_foos/foos.xml

Comparing t3 t4 · astridx boilerplate(2)

components/com_foos/View/Foo/HtmlView.php

Comparing t3 t4 · astridx boilerplate(1)

components/com_foos/tmpl/foo/default.php

Comparing t3 t4 · astridx boilerplate

Example in Joomla 4

Side Note

Why do we omit the closing PHP tag ?> at the end of a file?

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.

Why is it recommended to have empty line in the end of a source file?

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)

Test your component

Concluding Remark

next chapter

Overview of all files