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 4

Our goal is to display, in an HTML table, all data coming from a database table.

To do this we will build the view with its template and, in the next post, the corresponding model. In the end we will see how the installation package of the component is also able to make the necessary changes to the database, both during installation and removal of the component.

Creating the view

The code written so far is using the standard controller of our component, which loads the file controller.php and search inside it the class RegistryController, where Registry come from the name of the component that we created.

Continuing with the standardization, our controller provides, automatically, to load the view RegistryViewRegistry expecting to find it in the file views/registry/view.html.php.

This procedure is correct for small components which are limited to managing only one category of data, but since our component has slightly more ambitious aspirations, we need to use different views, depending on the category of data that we want to manage. In particular we believe correct to use, for the management of persons, the view persons, while another view may be used, for example, for companies.

Following this road we create the file views/persons/view.html.php with each missing folder, within our admin folder.

In view.html.php file, copy the following code:

<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
jimport( 'joomla.application.component.view' );
class RegistryViewPersons extends JView
{
    function display($tpl = null)
    {
        $this->text = 'Hello, World!';
        parent::display($tpl);
    }
}

We already know the first line, while jimport, in the second one, loads JView base class that we will soon extend. Remaining code is quite simple: JView display function is redefined storing the string to be displayed in an attribute of the class. This attribute is defined automatically and is therefore not necessary to declare it.

A final consideration on the file name: view.html.php. The html part of the name identifies the type of output that is generated and this too is part of some standard mechanisms of Joomla that the controller can manage using the appropriate URL parameters (in this case the parameter is format). In our case, as usual, we keep the default which is just html.

In the code we also find the $tpl parameter. It identifies the template to use and, if not specified, is called the standard template default.php.

Creation of the template

The template is responsible for generating the real html code. The file that implements it has to be created in views/persons/tmpl/default.php with the following contents:

<?php
// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );
?>
<h1><?=$this->text?></h1>

A simple file that prints the content of the text attribute as an H1 tag.

Note that in this case is not defined any class. The template is included directly by the class view, so the code inside it operates as if it were in a method of the class. Hence the reason why we can directly use the attributes defined in the view using $this.

Changing the controller

We complete the example by changing the controller previously realized. Take the controller.php file and modify it like this:

<?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)
    {
        // set default view if not set
        JRequest::setVar('view', JRequest::getCmd('view', 'persons'));
        
        // call parent behavior
        parent::display($cachable);
    }
}

We have removed the direct visualization of the message and call the display method that starts the display using the appropriate view.

We would not have even needed to redefine the display function, except that we want to set a default view when it is not specified by the appropriate parameter view. The first line added does this work by setting the URL parameter view  with the same value of this parameter, or, if is not specified, with the default value of "persons".

This call lets visualize every view specifying the view parameter or, in the lack of it, the default view (persons).

Creating the package

Before showing the full package, a consideration on files created. You may have noticed that in some cases is missing the php code ending tag "?>". This is done with the goal of preventing the insertion of additional empty lines in the generated code.

The structure on our PC will now be the following:

com_registry
  admin
    controller.php
    index.html
    registry.php
    views
       index.html
       persons
         index.html
         view.html.php
         tmpl
           default.php 
           index.html 
  site
    registry.php 
    index.html 
  registry.xml

Where, in addition to files already mentioned, have been inserted different empty index.html files.

The file registry.xml must 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.3</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>
            <folder>views</folder>
        </files>
    </administration>
</install>

Compared to the previous version has been updated the version number and introduced a tag to copy the whole folder views in admin.

Install the package and if everything works properly, you should be able to access the usual page of the component through the menus and display Hello, World!, this time with a bigger font size.

The package of what has been achieved so far can be downloaded from here.

Finally, try to invoke the following URL:

/administrator/index.php?option=com_registry&view=companies
/administrator/index.php?option=com_registry&format=text

The first one will produce an error saying that the view companies and the corresponding file views/companies/view.html.php cannot be found.

The second will load the right view persons, but will miss the specific view for the text format (the file should be views/persons/view.text.php).

This to explain that the standard Joomla! controller already does a great job in the management of possible actions to be performed and views to be presented based on standard parameters received via the URL.