Chapter: Define commons processes between many actions
« Command line processing | ^ Fundamental | Creating an administration interface » |
− Table of content
There are two type of treatments: business processes, and those to generate the web content. For business processes, you create some classes, which use jDao, working with a database. These classes can be used by all modules, so this is a way to execute some common processes. See the section about the use of business classes.
For the generation of web content, this is a little more complex because there are several ways of doing things. In most of cases, we have several pages that have some parts in common. This may be a banner, a footer, a menu on the side etc. .. There may be some pages which have more than these common "zone". Here are some various solutions, depending on the context, to create some common processes
- Using private methods of controllers
- Creating some controllers which will be inherited by others
- Using a common response object
- Using "zones"
Private methods of controllers ¶
If the similarity of pages can be summarized in a few actions that are in the same controller, then you can use a private or a protected method, which will do the common work of all these actions.
class defaultCtrl extends jController {
protected function common(){
$rep = $this->getResponse('html');
$rep->title = 'Same title on every page';
$rep->body>assign('menu', '<ul><li>item 1</li><li>item 2</li></ul>');
return $rep;
}
function index(){
$rep = $this->common();
$rep->body->assign('MAIN','<p>Welcome on this application</p>');
return $rep;
}
function list(){
$rep = $this->common();
$rep->body->assign('MAIN','<ul><li>an other</li><li>list</li></ul>');
return $rep;
}
}
In this instance, both actions index
and list
use a common method that
will do the common job.
base controllers ¶
If several actions of several controllers have some treatments in commons (but not all), then you can use the mechanism of object inheritance. This means you create a "base" controller, which will not be used directly, but other controllers will inherits from it.
Here is an example of a base controller, in the file controllers/myBaseController.php:
class myBaseController extends jController {
protected function common(){
$rep = $this->getResponse('html');
$rep->title = 'Same title on every page';
$rep->body>assign('menu', '<ul><li>item 1</li><li>item 2</li></ul>');
return $rep;
}
}
And in your controllers:
include jApp::coord()->getModulePath('monModule').'controllers/myBaseController.php';
class defaultCtrl extends myBaseController {
function index(){
$rep = $this->common();
$rep->body->assign('MAIN','<p>Welcome on this application</p>');
return $rep;
}
function list(){
$rep = $this->common();
$rep->body->assign('MAIN','<ul><li>an other</li><li>list</li></ul>');
return $rep;
}
}
Note the include instruction and extends myBaseController
. If the class
myBaseController
has been declared into the autoloading configuration, the
include instruction is useless.
Of course, the controller may contain basic properties, other methods, and also some methods which can be overrided.
The inheritance can be used to create for example a CRUD controller which implements many methods to create an interface to manage some datas (CRUD), and then, you have just to inherit from this controller to create quickly a CRUD interface. In fact, Jelix have a such controller to create CRUD interface.
Using zones ¶
A web page is built with several parts, and each part can be reused through many pages (not necessarily all). Those common blocks should be defined once (and cached optionnally) and be easily usable throughout your application.
In Jelix, this is achieved with zones.
Customizing common response ¶
Of course, if you have common content included on every pages, previous tips or objects are not very purposeful. You have to create a customized response by inheriting a default one. See chapter on the creation of customized responses.