JController and its subclass usage overview

From Joomla! Documentation

Quill icon.png
Page Actively Being Edited!

This article is actively undergoing a major edit for a short while.
As a courtesy, please do not edit this page while this message is displayed. The user who added this notice will be listed in the page history. This message is intended to help reduce edit conflicts; please remove it between editing sessions to allow others to edit the page. If this page has not been edited for several hours, please remove this template, or replace it with {{underconstruction}} or {{incomplete}}.

Preface[edit]

You should familiar with Joomla! 1.5 MVC pattern to start reading this topic. If not please, see others topics on this wiki.

Introduction[edit]

In Joomla! 1.6, there are lots of improvement for MVC. These improvements made developer task easier. In Joomla! 1.5, there is only one class for Controller named JController. In Joomla! 1.6, there are JControllerAdmin and JControllerForm as sublcass of JController. So there is a different usage for these three classes in Joomla! component development. This is a pattern used by most of Joomla! core components such as com_content, com_banners etc.

If you look at com_content backend, you will found two places for controller classes. The first one is in the root folder of component and the second one is in controllers folder. In the root folder there is a file named controller.php, this is a master controller of the component. It is a directly subclass of JController. In controllers folder, there are many files named according to entities in the component e.g. article and articles. These controllers subclass from JControllerAdmin or JControllerForm.

Master Controller[edit]

The master controller found in the root folder only responsibles for display task. This is default task for JController. If you look at JController::getInstance in com_content/content.php, only component name was passed as parameter. In JController, it use task variable (from request) to handle how to load Controller class. If task varible contains dot (.), it assume that this variable in the form of controller.task or controller.method. You should know that task variable specifies method of controller to run. Then in this case, it will load controller class in controllers folder and rewrite task variable to real task. If task does not come in dot format, JController will load master controller located in root folder as mentioned before. To simplify, we will refer to controller in controllers folder as subcontroller. You may notice that in master controller, default view is also specified there. If there is no view variable in the URL request, this one will be used. So in general, we can conclude that:-

  • To use master controller, specify view varaible as view name to use or default one will be used.
  • To use subcontroller, specify task varible in form of controller.task, clearly specified CONTROLL_NAME.CONTROLLER_METHOD.

We can categorised display into two types, the first one is presenting list of record or list view and the later one is showing record of item for user e.g. edit form. If wee look at articles view for com_content which displays list of records to user, we may notice that toolbar buttons were added according to user rights. For example, article edit, its task variable is article.edit. Recall that this request will load article subcontroller and execute its edit method. You may have a question, this mean we will display edit form to user. That is right! for general we do that but in Joomla! 1.6 core component, it just set article id to be edit in user session and redirect it to URL like &view=article&task=edit&id=1. And this will be handled by master controller and edit form will be presented to user. So in this case, all display will be handled by master controller and need views.

  • If no view name defined in URL, default view is used and default task (display) will be executed. We use this for display list of records. For example, index.php?option=com_content.
  • If view is defined but without task, master controller loads specified view and executes display method. This is also for display list of records. For example, index.php?option=com_content&view=articles.
  • If view is specified with task (but not in dot format), master controller loads specified view and execute method specified by task. This case is for display edit form, or present one item of record to user. Example is index.php?option=com_content&view=article&task=edit&id=20.

Subcontrollers[edit]

Subcontrollers will be handled all CRUD tasks. For tasks such as save, delete, publish that clearly understand that it doesnot need view, it just deletes or updates record and redirects user back to list view. Kinds of operation enable user to select atleast one or more record to process, such as delete or publish, inherited from JControllerAdmin.