Actions

User

Rvsjoen/tutorial/Developing an MVC Component/Part 07

From Joomla! Documentation

< User:Rvsjoen‎ | tutorial/Developing an MVC Component
Revision as of 09:33, 26 January 2012 by Rvsjoen (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Contents

Basic backend

Now that we have the basic set of the frontend features implemented, we would really like to be able to list and manage the hello messages in the table through the administrator interface. The first step in this quest is to create a controller, a view and a model for listing all the messages in the table. We also need to update our entry point for the backend to have it load the controller instead of just displaying our good old "Hello Admins" message.

Creating the controller

With your favorite editor, create the following file

admin/controller.php

<?php
// No direct access to this file
defined('_JEXEC') or die;
 
jimport('joomla.application.component.controller');
 
class HelloWorldController extends JController
{
        function display($cachable = false) 
        {
                // Set default view if not set
                JRequest::setVar('view', JRequest::getCmd('view', 'HelloWorlds')); 
                parent::display($cachable);
        }
}
?>

This controller does just a tiny bit more work than the controller we created for the frontend, and that is to set the default view if no other view is specified. This is needed since our default view will be called helloworlds and not helloworld. So the component name does not automatically get used as the default view name.

Updating the entry point

The entry point code is the same code as we used in the frontend, there is not really any reason to change this for basic functionality since all it really needs to do is to instantiate and execute the proper controller.

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

admin/helloworld.php

<?php
// No direct access to this file
defined('_JEXEC') or die;
 
jimport('joomla.application.component.controller');
 
// Get an instance of the controller prefixed by HelloWorld
$controller = JController::getInstance('HelloWorld');
 
// Perform the Request task
$controller->execute(JRequest::getCmd('task'));
 
// Redirect if set by the controller
$controller->redirect();
?>

Creating the model

Next thing on our list, is to create a model that can give us a list of all the greetings in the database.

With your favorite editor, create the following file

admin/models/helloworlds.php

<?php
// No direct access to this file
defined('_JEXEC') or die;
 
jimport('joomla.application.component.modellist');
 
class HelloWorldModelHelloWorlds extends JModelList
{
        protected function getListQuery() 
        {
                // Create a new query object.
                $db = JFactory::getDBO();
                $query = $db->getQuery(true);
 
                // Select some fields
                $query->select('id,greeting');
 
                // From the hello table
                $query->from('#__helloworld');
                return $query;
        }
}
?>

JModelList is a special extension of the JModel class which pretty much does all the hard work for us if we want to get a list of items, all we have to do is to override the function that provides the query, namely the getListQuery() function. The code in JModelList will handle the rest for us, isn't it great!

We use the database abstraction layer of Joomla! to build the query, we could also had written out the query in SQL and executed it directly, but by using the JDatabaseQuery object which is returned from $db->getQuery() it increases flexbility in the future as more databases might be supported.

If we would have written this query like before the query would be "SELECT id,greeting FROM #__helloworld".

Creating the view

Ok, so now that we have a model that is capable of providing us with a list of items, we can implement the view that is going to display them in a neat table with pagination.

With your favorite editor, create the following file

admin/views/helloworlds/view.html.php

<?php
 
// No direct access to this file
defined('_JEXEC') or die;
 
jimport('joomla.application.component.view');
 
class HelloWorldViewHelloWorlds extends JView
{
        function display($tpl = null) 
        {
                // Get data from the model
                $items = $this->get('Items');
                $pagination = $this->get('Pagination');
 
                // Assign data to the view
                $this->items = $items;
                $this->pagination = $pagination;
 
                // Display the template
                parent::display($tpl);
        }
}
?>

The view will call the appropriate getters on the model, and assign the data to the view, after which it will call the parent::display() function to render the view. Since no other layout has been specified the default view template will be used, so we need to create that, too.

With your favorite editor, create the following files

admin/views/helloworlds/tmpl/default.php

<?php
// No direct access to this file
defined('_JEXEC') or die;
 
JHtml::_('behavior.tooltip');
?>
<form action="<?php echo JRoute::_('index.php?option=com_helloworld'); ?>" method="post" name="adminForm">
        <table class="adminlist">
                <thead><?php echo $this->loadTemplate('head');?></thead>
                <tfoot><?php echo $this->loadTemplate('foot');?></tfoot>
                <tbody><?php echo $this->loadTemplate('body');?></tbody>
        </table>
</form>

admin/views/helloworlds/tmpl/default_head.php

<?php
// No direct access to this file
defined('_JEXEC') or die;
?>
<tr>
        <th width="5">
                ID
        </th>
        <th width="20">
                <input type="checkbox" name="toggle" value="" onclick="checkAll(<?php echo count($this->items); ?>);" />
        </th>
        <th>
                Greeting
        </th>
</tr>

admin/views/helloworlds/tmpl/default_body.php

<?php
// No direct access to this file
defined('_JEXEC') or die;
?>
<?php foreach($this->items as $i => $item): ?>
        <tr class="row<?php echo $i % 2; ?>">
                <td>
                        <?php echo $item->id; ?>
                </td>
                <td>
                        <?php echo JHtml::_('grid.id', $i, $item->id); ?>
                </td>
                <td>
                        <?php echo $item->greeting; ?>
                </td>
        </tr>
<?php endforeach; ?>

admin/views/helloworlds/tmpl/default_foot.php

<?php
// No direct access to this file
defined('_JEXEC') or die;
?>
<tr>
        <td colspan="3"><?php echo $this->pagination->getListFooter(); ?></td>
</tr>

While this might seem like an awful lot of files for such as simple task, there is a logic behind it. default.php is the base layout, and it will take care of loading the three subtemplates, namely _head, _body and _foot. This makes it easier to manage and it is very clear which part is the header, which part is the footer and which part creates the body of the view.

Installation manifest

The only updates to the manifest, include adding controller.php and views to the administrator file copy section, and updating the version number.

helloworld.xml

<?xml version="1.0" encoding="utf-8"?>
<extension type="component" version="2.5.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 recorded in the components table -->
        <version>0.0.7</version>
        <!-- The description is optional and defaults to the name -->
        <description>Description of the Hello World component ...</description>
 
        <install>
                <sql>
                        <file driver="mysql" charset="utf8">sql/install.mysql.utf8.sql</file>
                </sql>
        </install>
        <uninstall>
                <sql>
                        <file driver="mysql" charset="utf8">sql/uninstall.mysql.utf8.sql</file>
                </sql>
        </uninstall>
 
        <!-- 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>
                        <filename>controller.php</filename>
                        <folder>sql</folder>
                        <folder>tables</folder>
                        <folder>models</folder>
                        <folder>views</folder>
                </files>
        </administration>
 
</extension>

Testing your component

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 find the Hello World menu item on the Components menu. It should show you a nice table with all the hello messages that are currently stored in the database.

File listing

Download this part

Download example package

Articles in this series

This tutorial is supported by the following versions of Joomla!

Joomla 2.5