Rvsjoen/tutorial/Developing an MVC Component/Part 04
From Joomla! Documentation
< User:Rvsjoen | tutorial/Developing an MVC Component
Adding a model to the frontend[edit]
In Joomla! models are responsible for managing the data. The first function that has to be written for a model is a get function. It returns data to the caller. In our case, the caller will be the HelloWorldViewHelloWorld view. By default, the model named HelloWorldModelHelloWorld is the main model associated to this view.
site/models/helloworld.php
<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
jimport('joomla.application.component.modelitem');
class HelloWorldModelHelloWorld extends JModelItem
{
protected $msg;
/**
* Get the message
* @return string The message to be displayed to the user
*/
public function getMsg()
{
if (!isset($this->msg)) {
$this->msg = 'Hello World!';
}
return $this->msg;
}
}
Modifying the view[edit]
Now that we have a model, we can modify our view about so that instead of providing a hard-coded string as the hello messages, it will ask the model for the message to display.
The new file looks like this
Installation manifest[edit]
In addition to updating the version number, the models folder has been added to the files section.
helloworld.xml
<?xml version="1.0" encoding="utf-8"?>
<extension type="component" version="1.6.0" method="upgrade">
<name>Hello World!</name>
<!-- The following elements are optional and free of formatting constraints -->
<creationDate>June 2011</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 stored 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>
<!-- 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>
<menu>Hello World!</menu>
<!-- 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">
<filename>index.html</filename>
<filename>helloworld.php</filename>
</files>
</administration>
</extension>
Testing your component[edit]
For details on how to install the component into your Joomla! site, refer to the information provided in Part 01.
In order to test this component, go to the administrator interface and create a new menu item. In the menu item type selection interface, pick Hello World as the menu item type. Now you should be able to access this menu item in the frontend and it should show you the same result as when you entered the url directly in the previous part.
File listing[edit]
- helloworld.xml
- site/index.html
- site/helloworld.php
- site/controller.php
- site/views/index.html
- site/views/helloworld/index.html
- site/views/helloworld/view.html.php
- site/views/helloworld/tmpl/index.html
- site/views/helloworld/tmpl/default.php
- site/views/helloworld/tmpl/default.xml
- admin/index.html
- admin/helloworld.php
Download this part[edit]
Articles in this series[edit]
This tutorial is supported by the following versions of Joomla!
- Introduction
- Part 01 - Developing a Basic Component
- Part 02 - Adding a view to the frontend
- Part 03 - Adding a menu item type to the frontend
- Part 04 - Adding a model to the frontend
- Part 05 - Adding options to menu items
- Part 06 - Using a database
- Part 07 - Basic backend
- Part 08 - Adding language translation
- Part 09 - Adding actions to backend
- Part 10 - Adding decorations to the backend
- Part 11 - Adding validation
- Part 12 - Adding categories
- Part 13 - Adding component options
- Part 14 - Adding ACL
- Part 15 - Adding a script file