JController and its subclass usage overview

From Joomla! Documentation

Revision as of 17:48, 7 August 2012 by RetroCase (Talk | contribs)

Joomla 1.6Joomla 1.7Joomla 2.5



You should be familiar with the Joomla! 1.5 MVC pattern before reading this topic. If you are not, please read other topics on this wiki.



1.5 1.6 and later
JController JController


Master Controller

The master controller found in the root folder is only responsible for the display task. This is the default task for JController. If you look at JController::getInstance in com_content/content.php, only the component name was passed as a parameter. In JController, it uses the task variable (from the request) to determine what Controller class to load. If the task variable contains a dot (.), it assumes that this variable is 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 the task does not contain a dot (.), JController will load the master controller located in the root folder. To simplify, we will refer to controllers in 'controllers' folder as subcontroller. You may notice that in the master controller, a default view is also specified. If there is no view variable in the URL request, this one will be used. So in general, we can conclude:

  • To use the master controller, specify a view variable as view name to use or the default one will be used.
  • To use a subcontroller, specify the task variable in the form of controller.task, clearly specified CONTROLLER_NAME.CONTROLLER_METHOD.

We can categorize display into two types: presenting a list of records (list view) and showing a detailed record item to the user (e.g. edit form). If we look at the articles view in com_content which displays a list of records to the user, we may notice that toolbar buttons are added according to user rights.


 protected function addToolbar()
   if (($canDo->get('core.edit')) || ($canDo->get('core.edit.own'))) {

For example, to edit an article the task variable would be article.edit. This will cause the article subcontroller to be loaded and the edit method to be executed. You may have a question, this mean we will display edit form to user. That is right! In general we do that, but in Joomla! 1.6 core component, it just sets the article id to be edit in the user session and redirects it to an URL like &view=article&layout=edit. 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 needed views.





The other type of CRUD, such as add or edit, at a first glance should prepare data and present the form to user. But in the core component for Joomla! 1.6, they did not do that. Looking at this kind of subcontrollers (for example ContentControllerArticle) one can see that they always are inherited from JControllerForm. But no form was presented to user in this request, it just stores item (the ID of the article) in user's session variable and redirects to master controller. As a task variable was not specified, the display method is executed as a default task.
