J3.x:Developing an MVC Component/Adding a model to the site part
From Joomla! Documentation
< J3.x:Developing an MVC Component
Articles in This Series
- Introduction
- Developing a Basic Component
- Adding a View to the Site Part
- Adding a Menu Type to the Site Part
- Adding a Model to the Site Part
- Adding a Variable Request in the Menu Type
- Using the Database
- Basic Backend
- Adding Language Management
- Adding Backend Actions
- Adding Decorations to the Backend
- Adding Verifications
- Adding Categories
- Adding Configuration
- Adding ACL
- Adding an Install/Uninstall/Update Script File
- Adding a Frontend Form
- Adding an Image
- Adding a Map
- Adding AJAX
- Adding an Alias
- Using the Language Filter Facility
- Adding a Modal
- Adding Associations
- Adding Checkout
- Adding Ordering
- Adding Levels
- Adding Versioning
- Adding Tags
- Adding Access
- Adding a Batch Process
- Adding Cache
- Adding a Feed
- Adding an Update Server
- Adding Custom Fields
- Upgrading to Joomla4
This is a multiple-article series of tutorials on how to develop a Model-View-Controller Component for Joomla! Version.
Begin with the Introduction, and navigate the articles in this series by using the navigation button at the bottom or the box to the right (the Articles in this series).
Note
- This tutorial is part of the Developing a MVC Component for Joomla! 3.x tutorial. You are encouraged to read the previous parts of the tutorial before reading this.
- You can follow the steps below to add a model to the Hello World! component, or you can directly download the archive
- You can watch a video associated with this step in the tutorial at Step 4, Adding a Model.
Adding a Model to Hello World!
In this article we will cover how to add a model to a basic Joomla! component. For this example we will be continuing our work on the Hello World! component.
There are several ways to update a Joomla! component. As with the last tutorial, we will focus option 2.
1 | Manually add the files into <path_to_joomla>/ |
2 | Update using the Joomla! Extension Manager and the original directory, non-compressed, used for the component install |
3 | Update using the Joomla! Extension Manager and an Update Server |
To add a model you will need to navigate to com_helloworld, which is the original directory we made for our component. You must use the updated directory structure from the last tutorial. Using your preferred file manager, create or update the following files; as you create or update the files, add the source code for each file which is found in File Details.
1 | Create: helloworld.php | <path_to_com_helloworld>/site/models/helloworld.php |
2 | Create: index.html | <path_to_com_helloworld>/site/models/index.html |
3 | Update: view.html.php | <path_to_com_helloworld>/site/views/helloworld/view.html.php |
4 | Update: helloworld.xml | <path_to_com_helloworld>/helloworld.xml |
Updating the Hello World! Component
To update the Hello World! Component in the Joomla! website, please follow the same steps for the original installation.
File Details
site/models/helloworld.php
<?php
/**
* @package Joomla.Administrator
* @subpackage com_helloworld
*
* @copyright Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
/**
* HelloWorld Model
*
* @since 0.0.1
*/
class HelloWorldModelHelloWorld extends JModelItem
{
/**
* @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 World!';
}
return $this->message;
}
}
site/views/helloworld/view.html.php
<?php
/**
* @package Joomla.Administrator
* @subpackage com_helloworld
*
* @copyright Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
/**
* HTML View class for the HelloWorld Component
*
* @since 0.0.1
*/
class HelloWorldViewHelloWorld extends JViewLegacy
{
/**
* Display the Hello World view
*
* @param string $tpl The name of the template file to parse; automatically searches through the template paths.
*
* @return void
*/
function display($tpl = null)
{
// Assign data to the view
$this->msg = $this->get('Msg');
// Check for errors.
if (count($errors = $this->get('Errors')))
{
JLog::add(implode('<br />', $errors), JLog::WARNING, 'jerror');
return false;
}
// Display the view
parent::display($tpl);
}
}
helloworld.xml
<?xml version="1.0" encoding="utf-8"?>
<extension type="component" version="3.0" method="upgrade">
<name>Hello World!</name>
<!-- The following elements are optional and free of formatting constraints -->
<creationDate>January 2018</creationDate>
<author>John Doe</author>
<authorEmail>john.doe@example.org</authorEmail>
<authorUrl>http://www.example.org</authorUrl>
<copyright>Copyright Info</copyright>
<license>License Info</license>
<!-- The version string is recorded in the components table -->
<version>0.0.4</version>
<!-- The description is optional and defaults to the name -->
<description>Description of the Hello World component ...</description>
<update> <!-- Runs on update; New since J2.5 -->
<schemas>
<schemapath type="mysql">sql/updates/mysql</schemapath>
</schemas>
</update>
<!-- Site Main File Copy Section -->
<!-- Note the folder attribute: This attribute describes the folder
to copy FROM in the package to install therefore files copied
in this section are copied from /site/ in the package -->
<files folder="site">
<filename>index.html</filename>
<filename>helloworld.php</filename>
<filename>controller.php</filename>
<folder>views</folder>
<folder>models</folder>
</files>
<administration>
<!-- Administration Menu Section -->
<menu link='index.php?option=com_helloworld'>Hello World!</menu>
<!-- Administration Main File Copy Section -->
<!-- Note the folder attribute: This attribute describes the folder
to copy FROM in the package to install therefore files copied
in this section are copied from /admin/ in the package -->
<files folder="admin">
<!-- Admin Main File Copy Section -->
<filename>index.html</filename>
<filename>helloworld.php</filename>
<!-- SQL files section -->
<folder>sql</folder>
</files>
</administration>
</extension>
Code Explanation
In case you were curious as to why this works the way it does.
helloworld.php
class HelloWorldModelHelloWorld extends JModelItem
{
This class will be called by the class HelloWorldViewHelloWorld.
view.html.php
$this->msg = $this->get('Msg');
The HelloWorldViewHelloWorld class asks the model for data using the get method of the JViewLegacy. This get method converts the get('Msg') call into a getMsg() call on the model, which is the function which we have had to provide.
Component Contents
At this point in the tutorial, your component should contain the following files:
1 | helloworld.xml | this is an XML (manifest) file that tells Joomla! how to install our component. |
2 | site/helloworld.php | this is the site entry point to the Hello World! component |
3 | site/index.html | prevents web server from listing directory content |
4 | site/controller.php | file representing the controller |
5 | site/models/helloworld.php | file representing the model |
6 | site/models/index.html | prevents web server from listing directory content |
7 | site/views/index.html | prevents web server from listing directory content |
8 | site/views/helloworld/index.html | prevents web server from listing directory content |
9 | site/views/helloworld/view.html.php | file representing the view |
10 | site/views/helloworld/tmpl/index.html | prevents web server from listing directory content |
11 | site/views/helloworld/tmpl/default.php | the default view |
12 | site/views/helloworld/tmpl/default.xml | file that adds menu item |
13 | admin/index.html | prevents web server from listing directory content |
14 | admin/helloworld.php | this is the administrator entry point to the Hello World! component |
15 | admin/sql/index.html | prevents web server from listing directory content |
16 | admin/sql/updates/index.html | prevents web server from listing directory content |
17 | admin/sql/updates/mysql/index.html | prevents web server from listing directory content |
18 | admin/sql/updates/mysql/0.0.1.sql | file allowing to initialise schema version of the com_helloworld component. |
Please create a pull request or issue at https://github.com/joomla/Joomla-3.2-Hello-World-Component for any code descprepancies or if editing any of the source code on this page.