J1.5

Difference between revisions of "Developing a MVC Component/Creating an Administrator Interface"

From Joomla! Documentation

< J1.5:Developing a MVC Component
(Increased readability)
Line 85: Line 85:
  
 
==== Controller ====
 
==== Controller ====
The main controller of the admin section needs to deviate between the different Adds, Changes or Deletes that are requested, this is taken care of by creating sub controllers for each view.
+
The main controller of the admin section needs to deviate between the different Adds, Changes or Deletes that are requested, this is taken care of by creating sub controllers for each view for handling their specific tasks.
 
  <root>/administrator/controller.php
 
  <root>/administrator/controller.php
 
  <root>/administrator/controllers/users.php
 
  <root>/administrator/controllers/users.php
Line 91: Line 91:
 
  <root>/administrator/controllers/relation.php
 
  <root>/administrator/controllers/relation.php
  
The controller is an important player. Within the controller it is good to make a difference between '''activating tasks''' (for example the edit selection from a menu) and '''resulting tasks''' (for example the result of an edit trigger is the posted data).
+
The controller is an important part of the MVC pattern. Not only does it take care of the requested tasks, it is also the initiator of instantiating the model with the same name and it is responsible for setting the view and the desired form for that view. The controller really justifies its name.  
  
typical controller functions look like:
+
Within the controller it is good to make a difference between '''activating tasks''' (for example the edit selection from a menu) and '''resulting tasks''' (for example the result of an edit trigger is the posted data).
 +
 
 +
Typical controller functions look like:
 
<source lang="php">
 
<source lang="php">
 
function <activating task>()  // <-- edit, add, delete  
 
function <activating task>()  // <-- edit, add, delete  
Line 115: Line 117:
 
}
 
}
 
</source>
 
</source>
A controller takes care of all tasks for that dedicated controller. When completed in the controller, the resulting task will return to the main administration entrance point for that component; the default view. The activating tasks force a new view to display after first defining the View and Form.
+
A controller takes care of all tasks for that dedicated controller. After completing a resulting task the module will return to the main administration entrance point for that component, the main controller with the default view. Activating tasks enforce a new view with a form to display after first defining it.
 +
 
 +
The explicit definition of the form within a view might raise some eyebrows but our examples are too simple. For the general understanding and consistency this field should mostly be ''default''. In complex views multiple forms could be defined within one view.  
  
The explicit definition of the form might raise some eyebrows but our examples are too simple and this field should mostly be ''default''. In complex views one could hop from form to form for the same view.  
+
Note: In general it is good practice to maintain one form per view because the view.html.php still has to deliver the content. With some basic logic you can enable, disable certain content but if this logic is becoming too complex start considering splitting up the view. Sharing template parts (for uniform layouting of header and titles of your component) can be done using the PHP <code>include("../../tmpl_share/title.php");</code>.
  
Personal note: I personally prefer to maintain one form per view because the view.html.php still has to deliver the content. Giving the forms logical names is of cause handy when having a lot of different vies, having also that much default forms could make you easily loose oversight.
+
Note: Giving the forms logical in stead of the ''default'' naming is of cause handy when having a lot of different views. Having also that much ''default'' forms could make you easily loose oversight.
  
 
==== Models ====
 
==== Models ====

Revision as of 02:33, 12 August 2009

The "J1.5" namespace is an archived namespace. This page contains information for a Joomla! version which is no longer supported. It exists only as a historical reference, it will not be improved and its content may be incomplete and/or contain broken links.

Introduction[edit]

In the first three tutorials, we have developed a MVC component that retrieves its data from a table in the database. Currently, there is no way to add data to the database except to do it manually using another tool. In the next articles of this tutorial, we will develop an administrator section for our component which will make it possible to manage the entries in the database.

This article, Part 4 - Creating an Administrator Interface , will be an article with no new source code for our Hello component but we will go into some basic details of the MVC pattern. In the frontend solution (site section) we have developed the first part of our component. The frontend solution is based upon default Controllers, Views and Templates and you were taken by the hand to trust the default handling of the code. This is going to change in the Backend or Administration section of our Hello component.

Site / Admin[edit]

Joomla! is a content management system. The frontend is used for presenting the users with content, the backend is responsible for administrating the website framework (structuring / managing / controlling / maintaining). This deviation in presentation and administration is the fundamental of the Joomla! architecture.

Registered users are frontend content managers. They are allowed to change and add content but are not allowed to modify the structure of the website.

Enterance points[edit]

From the XML file of our frontend example it was already obvious that there would be an administration part:

<?xml version="1.0" encoding="utf-8"?>
  ...
  <administration>
  <!--  Administration Menu Section --> 
  <menu>Hello World!</menu> 
  <!--  Administration Main File Copy Section --> 
  <files folder="admin">
  <filename>hello.php</filename> 
  <filename>index.html</filename> 
  <filename>install.sql</filename> 
  <filename>uninstall.sql</filename> 
  </files>
  </administration>
  ...

Only the .sql files were of use and only during installation for our frontend view, the other two files have no content besides generating a blank page. Access with a terminal programm your life site at service provider (or on your own server) and do some browsing on your site after installing the frondend com_hello component. If you have clicked the right sub-directories you may have noticed that our Hello component is to be found twice:

<root>/components/com_hello
<root>/administrator/components/com_hello

Guest users and registered users will enter your site via the default root of the website:

<root>/index.php

Administrator users will have to login and after logging in they will enter your site via:

<root>/administrator/index.php

With the different access points it is easy to grasp that with setting up the administrator section the naming conventions have no dependency with the site section. Whilst the MVC pattern is also applicable for the administrator section this implies that identical Controls, Views and Models naming can (and sometimes must) be used as in the site section.

Tutorial specific naming[edit]

Within the next articles the explanation of this administrator section we will keep as close as possible to the component name. For the general overview, lists from the database, we will use Hellos as identification. The Hellos naming will be used for viewing and controlling multiple Hellos at once from the database. When selecting a single Hello for Editing or Adding we will use the singular Hello as naming for the Controller and View. This Admin Hello has no functional relation with the Site Hello (the only dependency is the database content of cause).

MVC pattern interaction[edit]

MVC joomla.png

In Developing a Model-View-Controller Component - Part 1 the figure on the right was used to explain the focus of the first three parts of this Developing a Model-View-Controller Component tutorial. Now we will use this figure to explain how decisions are made on what is going to be displayed and how to manipulate this.

Example roll-out[edit]

For explanation usage an easy to grasp example will be used. A library has the main function of lending books to registered users. Simply layed out three tables are applicable:

  • users
  • books
  • relation

Lets keep it all very simple. The users are addressed by Id and Name. The books are identified by Id and Title and the relation contains both Ids and date of lending.

Library Example.png

The example is carefully chosen and will help in explaining the Joomla! architecture in more detail. The administrative side of our Hello component is not even that complex with only one table to manage. After the explanation of this chapter it should be easy to follow the tutorial in the succeeding chapters

Mapping the example to Joomla![edit]

In this example we will assume that administrative actions (add, edit, delete) are tasks that are to be performed by the administrator. For the frontend user only the view of the Relation table is interesting ("when do I have to bring back the books?"). This example shows the entire list and ignores all privacy stuff that could be taken care of by letting registered users only see their own books (Hint: if ($user->registerd) {} ).

Just like our frontend Hello component, for this library component only the default view is being used in the frontend. It lists the relational table, left joining the other two tables to obtain a human readable list of lend books.

Alice | One flew over ... | 12 aug 2009
Alice | Celeb talk        | 12 aug 2009
Mark  | Joomla!           | 15 aug 2009

With respect to the administration part it is important to understand that we have one default and three dedicated views, each controlling three tasks:

  • <Component name default view>
  • User administration
    • Add
    • Change
    • Delete
  • Book administration
    • Add
    • Change
    • Delete
  • Relation administration
    • Add
    • Change
    • Delete

Controller[edit]

The main controller of the admin section needs to deviate between the different Adds, Changes or Deletes that are requested, this is taken care of by creating sub controllers for each view for handling their specific tasks.

<root>/administrator/controller.php
<root>/administrator/controllers/users.php
<root>/administrator/controllers/books.php
<root>/administrator/controllers/relation.php

The controller is an important part of the MVC pattern. Not only does it take care of the requested tasks, it is also the initiator of instantiating the model with the same name and it is responsible for setting the view and the desired form for that view. The controller really justifies its name.

Within the controller it is good to make a difference between activating tasks (for example the edit selection from a menu) and resulting tasks (for example the result of an edit trigger is the posted data).

Typical controller functions look like:

function <activating task>()  // <-- edit, add, delete 
{
    JRequest::setVar( 'view', '[<componentname> | users | books | relation ]' );
    JRequest::setVar( 'layout', 'default'  );     // <-- The default form is named here but in complex 
                                                  //     views multiple layouts might be needed.
    parent::display();
}

function <resulting task>()  // <-- save, remove
{
	$model = $this->getModel('[<componentname> | users | books | relation ]');
	if(!$model->action() ) {    // <-- action could be delete() or store()
		$msg = JText::_( 'Error: Could not perform action' );
	} else {
		$msg = JText::_( 'Action executed' );
	}

	$this->setRedirect( 'index.php?option=<componentname>', $msg );
}

A controller takes care of all tasks for that dedicated controller. After completing a resulting task the module will return to the main administration entrance point for that component, the main controller with the default view. Activating tasks enforce a new view with a form to display after first defining it.

The explicit definition of the form within a view might raise some eyebrows but our examples are too simple. For the general understanding and consistency this field should mostly be default. In complex views multiple forms could be defined within one view.

Note: In general it is good practice to maintain one form per view because the view.html.php still has to deliver the content. With some basic logic you can enable, disable certain content but if this logic is becoming too complex start considering splitting up the view. Sharing template parts (for uniform layouting of header and titles of your component) can be done using the PHP include("../../tmpl_share/title.php");.

Note: Giving the forms logical in stead of the default naming is of cause handy when having a lot of different views. Having also that much default forms could make you easily loose oversight.

Models[edit]

De Controllers interact with their equally named Model counter part. In the frontend view the Model was only used to retrieve data. The backend has more controllers and thus also more model files.

<root>/administrator/models/<componentname>.php
<root>/administrator/models/users.php
<root>/administrator/models/books.php
<root>/administrator/models/relation.php

Views[edit]

Separate views are of cause also required. For views and subsequent forms no forced naming conventions are required (linking to views is taken care of in the controller). In the following listing the Administrative tasks are identified as a subset for the different views but this choice is totally random and maybe even non-logical but that doesn't matter for the explanation. Just for example purposes I added a delete view that could be used for all the deletes for all administrative tasks asking an "Are you sure" display.

<root>/administrator/views/<componentname>/view.html.php    + .../tmpl/default.php
<root>/administrator/views/users/view.html.php    + .../tmpl/default.php
<root>/administrator/views/books/view.html.php    + .../tmpl/default.php
<root>/administrator/views/relation/view.html.php    + .../tmpl/default.php
 
<root>/administrator/views/delete/view.html.php    + .../tmpl/default.php

Essential Interaction Parameters[edit]

Everything is in place, just one big question remains: How to use them!

Three parameters are mandatory for letting Joomla! do its job:

  • option
  • controller
  • task

These three parameters are almost self explaining ;). The option part when developing a component is easy, always assign your component name to it. For component development consider this one as a constant, of cause the Joomla! engine has more options than only components.

The controller and task parameters can be manipulated within your component anyway you want it to.

How it all works together[edit]

Looking at the simplified MVC picture Joomla! the logical flow of interaction goes the following way:

  1. What is my entrance point?
    • The Joomla! engine discovers a logged in administrator and sets the entrance point to <root>/administrator/index.php otherwise it wil go to the <root>/index.php entrance.
  2. What option is requested?
    • The Joomla! engine reads the option variable and discovers that a component named <componentname> is requested. The entrance point becomes: <root>/administrator/modules/mod_<componentname>/<componentname>.php
  3. Is there need to access a dedicated controller?
    • The Joomla! engine reads the controller variable and discovers that a dedicated controller is required: <root>/administrator/modules/mod_<componentname>/controllers/<dedicatedcontroller>.php
  4. Is there a task that needs to be addressed?
    • The identified controller is handed the task value as parameter.
  5. The Model is derived from the controller and instantiated.
  6. The View and Form are set in the controller and initiated to become active.

How to add interaction[edit]

Within HTML there are two common ways to interact with Joomla!:

  1. reference to a link
  2. form submission post / get

Link reference for the Joomla! engine[edit]

Again the activating tasks and resulting task definitions drop by. Most activating tasks are initiated by a link. In case of the Library example the site section overview could be copied to the admin section and all cells could become links for editing the specific database element.

The first row could be programmed in a loop in the the template containing following code:

$link = JRoute::_( 'index.php?option=com_library&controller=users&task=edit&cid='. $row->idu );
echo "<td><a href=\"".$link."\">Alice </a></td>";
$link = JRoute::_( 'index.php?option=com_library&controller=books&task=edit&cid='. $row->idb );
echo "<td><a href=\"".$link."\">One flew over ...</a></td>";
$link = JRoute::_( 'index.php?option=com_library&controller=relation&task=edit&cid='. $row->idr );
echo "<td><a href=\"".$link."\">12 aug 2009</a></td>";

Within each click-able field the three mandatory parameters can be identified and one user parameter for handling the correct index in the controller task. These parameter are separated by '&'. Do not use spaces in your variables this might screw-up parameter handling in certain browsers.

[Alice} | [One flew over ...] | [12 aug 2009]
[Alice] | [Celeb talk]        | [12 aug 2009]
[Mark]  | [Joomla!]           | [15 aug 2009]

Posting Form Data to the Joomla! Engine[edit]

After being initiated by an activating task, an input form view might be the result. The sniplet code below could be the result of clicking the first cell of the default component view that is clicked for editing ([Alice]:cid=3).

<form action="index.php" method="post">
  <input type="text" name="username" id="username" size="32" maxlength="250" value="<?php echo $this->user->name;?>" />

  <input type="submit" name="SubmitButton" value="Save" />

  <input type="hidden" name="option" value="com_library" />                  // <-- mandatory
  <input type="hidden" name="controller" value="hello" />                    // <-- mandatory
  <input type="hidden" name="task" value="save" />                           // <-- mandatory
  <input type="hidden" name="id" value="<?php echo $this->user->id; ?>" />   // <-- user parameter, index in database for [Alice]
</form>

The three Joomla! mandatory parameters are placed as hidden in the form. Hidden parameters are not shown in any way but will be added to the posting data.

Tip: when debugging this form, replace in the form tag method="post" temporarily with method="get". All posted parameters will be shown in the URL of you browser. If the module is working undo this change. for one reason it is looking gmore sharp without all the parameters being shown in the URL. An other reason is to avoid motivating people to manipulate the browser URL themselves. Of cause one can look at the source code but using Post in stead of Get eliminates the first 90% of the earth population.

Remark: In some developed modules you may notice that developers have also added the view parameter. This is bad programming whilst the controller(s) should take care of the view and the form.

Conclusion[edit]

The use of the three mandatory parameters and the different access points are clarified. Let's do something with this knowledge and extend the Hello component to the administrative section in the succeeding articles of this tutorial.

Articles in this Series[edit]

Developing a Model-View-Controller Component - Part 1

Developing a Model-View-Controller Component - Part 2 - Adding a Model

Developing a Model-View-Controller Component - Part 3 - Using the Database

Developing a Model-View-Controller Component - Part 4 - Creating an Administrator Interface

Developing a Model-View-Controller Component - Part 5 - Basic Backend Framework

Developing a Model-View-Controller Component - Part 6 - Adding Backend Actions

Contributors[edit]

  • staalanden
  • jamesconroyuk