Quick links: Content - sections - sub sections
EN FR

Controller objects run actions corresponding to client-input. An action corresponds to a unique URL of your application.

Actions are methods of the controller that prepare a response.

Each controller is dedicated to one client-request type.

Naming conventions

File-system

Controllers are sorted per-module on the file-system, in the controllers directory. Each should have a unique prefix and suffix.

The file name of a controller should follow a specific format: ccc.ttt.php, where ccc is the controller name, and ttt is the request type that the controller is able to manage.

For example, the editor controller usable on classic client-requests: editor.classic.php

Class name

All controller class name should use suffix Ctrl.

For example, the class name of the editor controller should be: editorCtrl.

Creating a controller

Each module could define a default controller, which then should be named "default".

For example, the first controller that you might want to create, named "default": exampleModule/controllers/default.classic.php. The name of the class is then defaultCtrl. Here is the class:


class defaultCtrl extends jController {

}

Each controller class should inherit from jController. Then you add a method for each action you want to implement. Methods for actions should be public, without arguments, and should return an object which inherits from jResponse, so a "response" in Jelix. Example:


class defaultCtrl extends jController {

    public function index(){
        $resp = $this->getResponse('html');
        $resp->addContent('<p>Hello world</p>');
        return $resp;
    }
}

The method named "index" in a controller is always considered as the default action of the controller.

When Jelix cannot determine the action and/or the controller from the URL, it tries to execute the method index() of the "default" controller. So it is recommended to have a "default" controller in a module, and an index() method in each controller.

Redefining the class constructor

To redefine the class constructor, you should call the parent constructor (so the constructor of jController), by giving the received argument, which is a jRequest object. Example:


class defaultCtrl extends jController {
    function __construct($request) {
        //...
        parent::__construct($request);
        //...
    }
}

Services provided by jController

jController provides methods for your actions.

Fetch a jResponse object

Actions should always return jResponse objects which implements the "View" of the MVC pattern. Jelix supplies several response objects, which allow to generate easily some HTML pages, JSON, text, XML, Zip, redirections etc, and you can make your own inheriting from jResponse.

Each response type is declared with an alias. So, to retrieve a specific response object, you call the method getResponse() by indicating the alias corresponding to the response type:


   $resp = $this->getResponse('html');

$resp is now an instance of jResponseHtml capable of generating HTML.

Notice that you can create your own response object. You can then support a new output format, or you can overload an existing response object to do some things shared between many actions. For example, you can create your own response HTML which generates dynamically a menu. Then, in all actions where you use this responses, you will have this menu. See the page which explains how to do this.