Difference between revisions of "JController and its subclass usage overview"

From Joomla! Documentation

m
Line 1: Line 1:
 +
{{In Use}}
 +
 +
=== Preface ===
 +
You should familiar with Joomla! 1.5 MVC pattern to start reading this topic. If not please, see others topics on this wiki.
 +
=== Introduction ===
 
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.
 
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.
 
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 ===
 
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:-
 
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 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.
 
* To use subcontroller, specify task varible in form of controller.task, clearly specified CONTROLL_NAME.CONTROLLER_METHOD.

Revision as of 01:27, 20 January 2011

Template:In Use

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.