Actions

J1.5

Difference between revisions of "Developing a MVC Component/Adding a Model"

From Joomla! Documentation

< J1.5:Developing a MVC Component
(Using the Model)
m (Download: clean up and recategorisation if necessary)
(37 intermediate revisions by 14 users not shown)
Line 1: Line 1:
====== Developing a Model-View-Controller Component - Part 2 - Adding a Model ======
+
{{Chunk:Developing a Model-View-Controller (MVC) Component for Joomla!1.5 - Contents}}
 
+
== Introduction ==
===== Introduction =====
+
  
 
In the first tutorial of this series, creating a simple view-controller component using the Joomla! 1.5 CMS framework was demonstrated.
 
In the first tutorial of this series, creating a simple view-controller component using the Joomla! 1.5 CMS framework was demonstrated.
Line 9: Line 8:
 
In this second part of the tutorial we will demonstrate how to move this out of the view and into a model. In future tutorials we will demonstrate the power and flexibility that this design pattern provides.
 
In this second part of the tutorial we will demonstrate how to move this out of the view and into a model. In future tutorials we will demonstrate the power and flexibility that this design pattern provides.
  
===== Creating the Model =====
+
== Creating the Model ==
  
 
The concept of model gets its name because this class is intended to represent (or 'model') some entity. In our case, our first model will represent a 'hello', or a greeting. This is in line with our design thus far, because we have one view ('hello'), which is a view of our greeting.
 
The concept of model gets its name because this class is intended to represent (or 'model') some entity. In our case, our first model will represent a 'hello', or a greeting. This is in line with our design thus far, because we have one view ('hello'), which is a view of our greeting.
  
The naming convention for models in the Joomla! framework is that the class name starts with the name of the component (in our case 'hello', followed by 'model', followed by the model name. Therefore, our model class is called HelloModelHello.
+
The naming convention for models in the Joomla! framework is that the class name starts with the name of the component (in our case 'hello'), followed by 'model', followed by the model name. Therefore, our model class is called HelloModelHello.
  
 
At this point, we will only model one behaviour of our hello, and that is retrieving the greeting. We will thus have one method, called getGreeting(). It will simply return the string 'Hello, World!'.
 
At this point, we will only model one behaviour of our hello, and that is retrieving the greeting. We will thus have one method, called getGreeting(). It will simply return the string 'Hello, World!'.
  
Here is the code for our model class:
+
The code for the model at site/models/hello.php:
 
+
 
   
 
   
 
<source lang="php"><?php
 
<source lang="php"><?php
Line 26: Line 24:
 
  * @package    Joomla.Tutorials
 
  * @package    Joomla.Tutorials
 
  * @subpackage Components
 
  * @subpackage Components
  * @link http://dev.joomla.org/component/option,com_jd-wiki/Itemid,31/id,tutorials:modules/
+
  * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_2
 
  * @license    GNU/GPL
 
  * @license    GNU/GPL
 
  */
 
  */
  
// Check to ensure this file is included in Joomla!
+
// No direct access
defined('_JEXEC') or die();
+
 
 +
defined( '_JEXEC' ) or die( 'Restricted access' );
  
 
jimport( 'joomla.application.component.model' );
 
jimport( 'joomla.application.component.model' );
Line 57: Line 56:
 
Now that we have created our model, we must modify our view so that it uses it to obtain the greeting.
 
Now that we have created our model, we must modify our view so that it uses it to obtain the greeting.
  
===== Using the Model =====
+
== Using the Model ==
  
The Joomla! framework is setup in such a way that the controller will automatically load the model that has the same name as the view and will push it into the view. Since our view is called 'Hello', our 'Hello' model will automatically be loaded and pushed into the view. Therefore, we can easily retrieve a reference to our model using the JView::getModel() method.
+
The Joomla! framework is setup in such a way that the controller will automatically load the model that has the same name as the view and will push it into the view. Since our view is called 'Hello', our 'Hello' model will automatically be loaded and pushed into the view. Therefore, we can easily retrieve a reference to our model using the JView::getModel() method. (If the model had not followed this convention, we could have passed the model name, as a parameter, to [http://api.joomla.org/Joomla-Framework/Application/JView.html#getModel JView::getModel()])
  
 
Our previous view code contained the lines:
 
Our previous view code contained the lines:
 
 
   
 
   
<source>$greeting = "Hello World!";</source>
+
<source lang="php">$greeting = "Hello World!";</source>
  
 
To take advantage of our model, we change this line to:
 
To take advantage of our model, we change this line to:
 
 
   
 
   
<source lang="php">$model =& $this->getModel();
+
<source lang="php">$model = &$this->getModel();
 
$greeting = $model->getGreeting();</source>
 
$greeting = $model->getGreeting();</source>
  
 
The complete view now looks like:
 
The complete view now looks like:
 
 
   
 
   
 
<source lang="php"><?php
 
<source lang="php"><?php
 
 
/**
 
/**
* Hello View for Hello World Component
 
*
 
 
  * @package    Joomla.Tutorials
 
  * @package    Joomla.Tutorials
 
  * @subpackage Components
 
  * @subpackage Components
  * @link http://dev.joomla.org/component/option,com_jd-wiki/Itemid,31/id,tutorials:modules/
+
  * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_2
 
  * @license    GNU/GPL
 
  * @license    GNU/GPL
*/
+
*/
  
// no direct access
+
// No direct access
  
 
defined( '_JEXEC' ) or die( 'Restricted access' );
 
defined( '_JEXEC' ) or die( 'Restricted access' );
Line 95: Line 88:
 
  * HTML View class for the HelloWorld Component
 
  * HTML View class for the HelloWorld Component
 
  *
 
  *
  * @package    Joomla.Tutorials
+
  * @package    HelloWorld
* @subpackage Components
+
 
  */
 
  */
  
Line 103: Line 95:
 
     function display($tpl = null)
 
     function display($tpl = null)
 
     {
 
     {
         $model =& $this->getModel();
+
         $model = &$this->getModel();
 
         $greeting = $model->getGreeting();
 
         $greeting = $model->getGreeting();
 
         $this->assignRef( 'greeting', $greeting );
 
         $this->assignRef( 'greeting', $greeting );
Line 109: Line 101:
 
         parent::display($tpl);
 
         parent::display($tpl);
 
     }
 
     }
}
+
}</source>
?></source>
+
  
==== Adding the File to the Package ====
+
=== Adding the File to the Package ===
  
 
All that remains is to add an entry to the XML file so that our new model will be copied. The Joomla! framework will look for our model in the models directory, so the entry for this file will look like (it should be added to the site section):
 
All that remains is to add an entry to the XML file so that our new model will be copied. The Joomla! framework will look for our model in the models directory, so the entry for this file will look like (it should be added to the site section):
  
+
<source lang="php"><filename>models/hello.php</filename></source>
<source><filename>models/hello.php</filename></source>
+
  
 
Our new hello.xml file will look like:
 
Our new hello.xml file will look like:
 
 
   
 
   
<source><?xml version="1.0" encoding="utf-8"?>
+
<source lang="xml"><?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE install SYSTEM "http://dev.joomla.org/xml/1.5/component-install.dtd">
+
 
<install type="component" version="1.5.0">
 
<install type="component" version="1.5.0">
<name>Hello</name>
+
<name>Hello</name>
    <!-- The following elements are optional and free of formatting conttraints -->
+
<!-- The following elements are optional and free of formatting constraints -->
    <creationDate>2007 02 22</creationDate>
+
<creationDate>2007-02-22</creationDate>
    <author>John Doe</author>
+
<author>John Doe</author>
    <authorEmail>john.doe@example.org</authorEmail>
+
<authorEmail>john.doe@example.org</authorEmail>
    <authorUrl>http://www.example.org</authorUrl>
+
<authorUrl>http://www.example.org</authorUrl>
    <copyright>Copyright Info</copyright>
+
<copyright>Copyright Info</copyright>
    <license>License Info</license>
+
<license>License Info</license>
    <!--  The version string is recorded in the components table -->
+
<!--  The version string is recorded in the components table -->
    <version>Component Version String</version>
+
<version>1.01</version>
    <!-- The description is optional and defaults to the name -->
+
<!-- The description is optional and defaults to the name -->
    <description>Description of the component ...</description>
+
<description>Description of the component ...</description>
 +
 
 +
<!-- 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>controller.php</filename>
 +
  <filename>hello.php</filename>
 +
  <filename>index.html</filename>
 +
  <filename>models/hello.php</filename>
 +
  <filename>models/index.html</filename>
 +
  <filename>views/index.html</filename>
 +
  <filename>views/hello/index.html</filename>
 +
  <filename>views/hello/view.html.php</filename>
 +
  <filename>views/hello/tmpl/default.php</filename>
 +
  <filename>views/hello/tmpl/index.html</filename>
 +
</files>
 +
 
 +
<administration>
 +
  <!-- Administration Menu Section -->
 +
  <menu>Hello World!</menu>
 +
 
 +
  <!-- Administration Main File Copy Section -->
 +
  <files folder="admin">
 +
  <filename>hello.php</filename>
 +
  <filename>index.html</filename>
 +
  </files> 
  
    <!-- Site Main File Copy Section -->
+
</administration>
    <files folder="site">
+
        <filename>index.html</filename>
+
        <filename>hello.php</filename>
+
        <filename>controller.php</filename>
+
        <filename>views/index.html</filename>
+
        <filename>views/hello/index.html</filename>
+
        <filename>views/hello/view.html.php</filename>
+
        <filename>views/hello/tmpl/index.html</filename>
+
        <filename>views/hello/tmpl/default.php</filename>
+
        <filename>models/index.html</filename>
+
        <filename>models/hello.php</filename>
+
    </files>
+
   
+
    <administration>
+
        <!-- Administration Menu Section -->
+
        <menu>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">
+
    <!-- Site Main File Copy Section -->
+
            <filename>index.html</filename>
+
            <filename>admin.hello.php</filename>
+
        </files>       
+
    </administration>
+
 
</install></source>
 
</install></source>
  
===== Conclusion =====
+
== Conclusion ==
  
 
We now have a simple MVC component. Each element is very simple at this point, but provides a great deal of flexibility and power.
 
We now have a simple MVC component. Each element is very simple at this point, but provides a great deal of flexibility and power.
  
===== Contributors =====
+
== Contributors ==
 +
* staalanden
  
  * staalanden
+
== Download ==
  
===== Download =====
+
The component can be downloaded at: [http://joomlacode.org/gf/download/frsrelease/8109/29434/com_hello2_01.zip com_hello2_01]
  
The component can be downloaded at: [[http://dev.joomla.org/components/com_jd-wiki/data/media/components/com_hello2.zip|com_hello2.zip]]
+
[[Category:Component Development]]

Revision as of 08:23, 18 September 2012

Replacement filing cabinet.png
This Namespace has been archived - Please Do Not Edit or Create Pages in this namespace. Pages contain information for a Joomla! version which is no longer supported. It exists only as a historical reference, will not be improved and its content may be incomplete.

Contents

Introduction

In the first tutorial of this series, creating a simple view-controller component using the Joomla! 1.5 CMS framework was demonstrated.

In the first tutorial, the greeting was hardcoded into the view. This doesn't follow the MVC pattern exactly because the view is intended to only display the data, and not contain it.

In this second part of the tutorial we will demonstrate how to move this out of the view and into a model. In future tutorials we will demonstrate the power and flexibility that this design pattern provides.

Creating the Model

The concept of model gets its name because this class is intended to represent (or 'model') some entity. In our case, our first model will represent a 'hello', or a greeting. This is in line with our design thus far, because we have one view ('hello'), which is a view of our greeting.

The naming convention for models in the Joomla! framework is that the class name starts with the name of the component (in our case 'hello'), followed by 'model', followed by the model name. Therefore, our model class is called HelloModelHello.

At this point, we will only model one behaviour of our hello, and that is retrieving the greeting. We will thus have one method, called getGreeting(). It will simply return the string 'Hello, World!'.

The code for the model at site/models/hello.php:

<?php
/**
 * Hello Model for Hello World Component
 * 
 * @package    Joomla.Tutorials
 * @subpackage Components
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_2
 * @license    GNU/GPL
 */
 
// No direct access
 
defined( '_JEXEC' ) or die( 'Restricted access' );
 
jimport( 'joomla.application.component.model' );
 
/**
 * Hello Model
 *
 * @package    Joomla.Tutorials
 * @subpackage Components
 */
class HelloModelHello extends JModel
{
    /**
    * Gets the greeting
    * @return string The greeting to be displayed to the user
    */
    function getGreeting()
    {
        return 'Hello, World!';
    }
}

You will notice a line that starts with jimport. The jimport function is used to load files from the Joomla! framework that are required for our component. This particular statement will load the file /libraries/joomla/application/component/model.php. The '.'s are used as directory separators and the last part is the name of the file to load. All files are loaded relative to the libraries directory. This particular file contains the class definition for the JModel class, which is necessary because our model extends this class.

Now that we have created our model, we must modify our view so that it uses it to obtain the greeting.

Using the Model

The Joomla! framework is setup in such a way that the controller will automatically load the model that has the same name as the view and will push it into the view. Since our view is called 'Hello', our 'Hello' model will automatically be loaded and pushed into the view. Therefore, we can easily retrieve a reference to our model using the JView::getModel() method. (If the model had not followed this convention, we could have passed the model name, as a parameter, to JView::getModel())

Our previous view code contained the lines:

$greeting = "Hello World!";

To take advantage of our model, we change this line to:

$model = &$this->getModel();
$greeting = $model->getGreeting();

The complete view now looks like:

<?php
/**
 * @package    Joomla.Tutorials
 * @subpackage Components
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_2
 * @license    GNU/GPL
*/
 
// No direct access
 
defined( '_JEXEC' ) or die( 'Restricted access' );
 
jimport( 'joomla.application.component.view');
 
/**
 * HTML View class for the HelloWorld Component
 *
 * @package    HelloWorld
 */
 
class HelloViewHello extends JView
{
    function display($tpl = null)
    {
        $model = &$this->getModel();
        $greeting = $model->getGreeting();
        $this->assignRef( 'greeting',   $greeting );
 
        parent::display($tpl);
    }
}

Adding the File to the Package

All that remains is to add an entry to the XML file so that our new model will be copied. The Joomla! framework will look for our model in the models directory, so the entry for this file will look like (it should be added to the site section):

<filename>models/hello.php</filename>

Our new hello.xml file will look like:

<?xml version="1.0" encoding="utf-8"?>
<install type="component" version="1.5.0">
 <name>Hello</name>
 <!-- The following elements are optional and free of formatting constraints -->
 <creationDate>2007-02-22</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>1.01</version>
 <!-- The description is optional and defaults to the name -->
 <description>Description of the component ...</description>
 
 <!-- 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>controller.php</filename>
  <filename>hello.php</filename>
  <filename>index.html</filename>
  <filename>models/hello.php</filename>
  <filename>models/index.html</filename>
  <filename>views/index.html</filename>
  <filename>views/hello/index.html</filename>
  <filename>views/hello/view.html.php</filename>
  <filename>views/hello/tmpl/default.php</filename>
  <filename>views/hello/tmpl/index.html</filename>
 </files>
 
 <administration>
  <!-- Administration Menu Section -->
  <menu>Hello World!</menu>
 
  <!-- Administration Main File Copy Section -->
  <files folder="admin">
   <filename>hello.php</filename>
   <filename>index.html</filename>
  </files>  
 
 </administration>
</install>

Conclusion

We now have a simple MVC component. Each element is very simple at this point, but provides a great deal of flexibility and power.

Contributors

  • staalanden

Download

The component can be downloaded at: com_hello2_01