User

Difference between revisions of "Rvsjoen/tutorial/Developing an MVC Component/Part 04"

From Joomla! Documentation

< User:Rvsjoen‎ | tutorial/Developing an MVC Component
Line 146: Line 146:
  
 
== Download this part ==
 
== Download this part ==
 +
 +
[https://github.com/downloads/rvsjoen/joomla-tutorials/com_helloworld-part04.zip Download example package]
  
 
== Articles in this series ==
 
== Articles in this series ==

Revision as of 11:18, 14 August 2011

Adding a model to the frontend[edit]

In Joomla!, models are responsible for managing the data. This means that in order to implement our model, we need some kind of function in it that returns data to whoever calls it. In our component the caller will be the HelloWorld view. To keep in line with certain unwritten naming conventions, we name this function getItem() since what we are getting from the model is actually a single item (a hello message). So let's move on to writing some code.

Creating a model[edit]

By default, the model named HelloWorldModelHelloWorld is the main model associated to this view. Whenever the view calls $this->get('something') it will eventually end up calling some function named getSomething() in its default model.

With your favorite editor, create the following file

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 $item;

        /**
         * Get the message
         * @return string The message to be displayed to the user
         */
        public function getItem() 
        {
                if (!isset($this->item)) {
                        $this->item = 'Hello World!';
                }
                return $this->item;
        }
}

The reason we are extending from JModelItem instead of just JModel is that JModelItem provides a lot of functionality for handling a single item, which is what we want. This saves us from having to implement this ourselves.

Modifying the view[edit]

Now that we have a model that we can bug to get our data, we can modify our view about so that instead of providing a hard-coded string as the hello message, it will call the model to get the message to display.

With your favorite editor, modify the following file to look like this

site/views/helloworld/view.html.php

<?php
// No direct access to this file
defined('_JEXEC') or die;

jimport('joomla.application.component.view');

class HelloWorldViewHelloWorld extends JView
{
        function display($tpl = null) 
        {
                // Assign data to the view
                $this->item = $this->get('item');

                // Display the view
                parent::display($tpl);
        }
}

As you can see, there is only one change from the previous view.html.php and that is, instead of directly assigning to $this->item we call $this->get('item') which is really a call to the model which belongs to this view. The magic of instantiating the model and correctly calling it is not covered in this tutorial, this is all done for you.

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]

Download this part[edit]

Download example package

Articles in this series[edit]

This tutorial is supported by the following versions of Joomla!

Joomla 2.5