LOGIC Library

This site is the Logic involvement in sharing expertise and skills acquired in daily work. The goal is to create a solid knowledge base and share best practices in software development and systems management.

More info about us can be found on logicsistemi.it.

Developing MVC components for Joomla! 2.5 - Part 3

The MVC development pattern is widely used in modern programming languages, as it has the undoubted advantage to clearly separate the management of data and their presentation to the user. This makes  particularly easy to maintain the code and provide alternate views of information, aspect that is becoming more important with the spread of devices with different characteristics.

You can find more detailed information on the operation of this pattern in the traditional bibliography or, to stay connected to our CMS, on the Joomla! official documentation site.

For our purposes I carry out a short explanation of the components of this pattern, with particular reference to the Joomla! implementation.

The controller analyzes the parameters of the URL and on their basis decides which action to perform and wich view to display. The parameter that we will soon introduce is task.

The model contains logic that allows data interaction. It runs the code to read and modify data stored in a database or other source. In Joomla! direct access to the database is normally delegated to the table, leaving to the model only the "business logic".

The view should take care of the formatting information for the presentation to the user. Joomla! separates the model interaction from the realization of the output by introducing the concept of templates. This is particularly useful to easily customize the graphics of the returned HTML page templates to change the overall site template.
With this simplification, the view is limited to query the model and store the data so that the template has them available.

The table mentioned above is only a structure that simplifies access to the database allowing us not to write SQL queries to interact with the data.

Activation of the MVC behavior

Let's start the explanation using a standard case where, as we will see, Joomla! makes extensive use of defaults.

Suppose you are calling the URL previously used to test our component, which leads to the execution of the file registry.php:

/administrator/index.php?option=com_registry

In the file registry.php we must enter the code that instantiates the controller so that it starts the MVC pattern. The code to enter is the following:

<?php
// No direct access
defined('_JEXEC') or die('Restricted access');
// Require the base controller
jimport('joomla.application.component.controller');
// Create the controller
$controller = JController::getInstance('Registry');
// Perform the Request task
$controller->execute( JRequest::getCmd('task') );
// Redirect if set by the controller
$controller->redirect();

The first line only prevents direct execution of the file by a user call, without going through the mechanisms of Joomla.

The line that starts with jimport loads JController class. To reduce system load Joomla! does not automatically load all the library code, but we must ask to do that using this syntax.

Is then instantiated the controller class of our component. The name passed is the name of our component and is the prefix for all classes that we will create. Our controller, therefore, will be called RegistryController.

With the execute method is performed the operation indicated by the task parameter, obtained by a suitable method of Joomla that also filters hackers attacks with malformed URLs. If this parameter is not specified is executed the operation display. From the point of view of the code is called the display method of our controller.

After certain operations should be necessary to redirect to a different URL (for example, after removing a record we want to come back to the list of data), the method called is redirect that performs a redirection to the URL set by the controller.

The controller

We said that Joomla! expects to find a class RegistryController, but where this class is sought? By default Joomla! expects to find it in the controller.php file located in the same folder of registry.php. Therefore we try to create it with this content:

<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
// import Joomla controller library
jimport('joomla.application.component.controller');
/**
 * General Controller of Registry component
 */
class RegistryController extends JController
{
    public function display($cachable = false, $urlparams = false)
    {
        echo "Hello, World!";
    }
}

In the class has been redefined the display method because we have not yet created the view and the default display function would generate an error. In the next post we will create the view and delete this temporary function.

To try everything out you can simply copy files registry.php and controller.php in /administrator/components/com_registry, or rebuild the packege as explained below.

If you want to do further tests to better understand the mechanism, create a test function that displays a different sentence and try to retrieve the URL:

/administrator/index.php?option=com_registry&task=test

it should visualize the new sentence. Also try to put the controller without any method, so you can see the error message reporting that the view cannot be found.

Package for the component update

On our PC we have the following structure:

com_registry
  admin
    controller.php
    index.html
    registry.php
  site
    index.html
    registry.php
  registry.xml

The registry.xml file should be modified in the following way:

<?xml version="1.0" encoding="UTF-8"?>
<install type="component" version="2.5.0" method="upgrade">
    <name>Registry</name>
    <author>Edy Incoletti</author>
    <authorEmail>edy.incoletti@logicsistemi.it</authorEmail>
    <authorUrl>http://library.logicsistemi.it</authorUrl>
    <creationDate>March 2012</creationDate>
    <version>0.0.2</version>
    <description>Registry management for the tutorial.</description>
    <files folder="site">
        <filename>index.html</filename>
        <filename>registry.php</filename>
    </files>
    <administration>
        <files folder="admin">
            <filename>index.html</filename>
            <filename>registry.php</filename>
            <filename>controller.php</filename>
        </files>
    </administration>
</install>

Compared to the previous version was added the method attribute to the install tag and changed the version number. The installation of Joomla! allows you to update the packages and this operation is is based on version number. If we had not specified the upgrade method, since our component is already installed, the installation operation would do nothing. If the version number was not changed would not have done anything again.

At the end we added the new file controller.php.

We create the usual zip you can download from here and try the installation.

You can try to enter the usual URL in your browser or use the com-registry menu entry under Components in the Joomla! administration menu. This item was automatically created during the installation.

Comments   

 
#1 kamaljit laishram 2013-07-19 12:05
so far i enjoying this tutorial
thanks
Quote
 
 
#2 Siva 2015-01-13 14:50
when install the component,I am getting the error "
JInstaller: :Install: Cannot find Joomla XML setup file ", Please help me
Quote
 
 
#3 Steve 2015-02-09 19:40
Need to edit the registry.xml file and change the and to be etc
Quote