Quick links: Content - sections - sub sections
EN FR

If you have to deal with an unsupported output format in Jelix, just create a new response.

You have to follow these steps:

  1. create a class inheriting from jResponse (or another existing response), and implementing methods to support your specific format.
  2. save this class in a file located under the app/responses/ application folder or also in the folder responses/ of your own module, or in an other directory of your module if the class has a namespace and is auto-loaded.
  3. declare the new response type in configuration file and assign its alias.

Create a class

A response class must declare at min two methods: output() and outputErrors(). And overload default value of $_type member with its specific response type. output() generates response content by means of call to print or echo. outputErrors() is called if blocking errors are raised through action processing. A different response is then generated maybe respecting the specific format also. And do not forget to specify the format type in HTTP header.

Example (it's a totally funny format, of course):



class myFooResponse extends jResponse {
    protected $_type = 'foo';

    public $content = '';

    /**
     * generate content and send it to the client.
     * @return boolean    true if process is ok, false otherwise
     */
    public function output(){
        $this->_httpHeaders['Content-Type']='text/foo;charset='.jApp::config()->charset;
        $this->sendHttpHeaders();
        echo "content:\n".$this->content."/content;";
        return true;
    }

    public function outputErrors(){
        header('Content-Type: text/foo;charset='.jApp::config()->charset);
        echo "errors\n";
        foreach (jApp::coord()->errorMessages  as $e){
            echo '['.$e[0].' '.$e[1].'] '.$e[2]." \t".$e[3]." \t".$e[4]."\n";
        }
        echo "/errors;";
    }
}

As shown above, you can added any new member properties and methods to your response (such as $content) to deal with your format. For example, jResponseHtml defines an API that you can use by default: it instantiates a template, it has a title property for setting an HTML page title, it defines methods to fill the <head> block, etc.

Dive into lib/jelix/core/response/ and pick a response class to inherit if your format is closed to this existing response.

Class location

There is no naming convention for your response class but the filename should be the class name followed by .class.php. In the example above, the class should live in a myFooResponse.class.php file under app/responses/ folder of your application or in responses/ of a module.

It is also possible to use a class name with a namespace, stored into a file with a name allowing to be auto-loaded (by declaring the class name into the module.xml or composer.json file), into any directory of a module.

Declare a response

Last step, your new response must be declared and given an alias in configuration file. Just add this line <alias>=<class> in [responses] section of config.ini.php.

Here is an example of a response for your application:


[responses]
foo=myFooResponse

Here is an example of a response from your module:


[responses]
foo="mymodule~myFooResponse"

Or if the class can be autoloaded by its namespace:


[responses]
foo="\My\Foo\Response\Class"

Then in a controller, you can use myFooResponse:



function index(){
   $rep = this->getResponse('foo');
   $rep->content='hello world';
   return $rep;
}

Note: Jelix uses aliases for responses. It allows to redefine transparently a response type and customize it. See common processes documentation.

About the use of templates

In templates, you have to use some plugins like functions. Template plugins are related to a specific type of response. So if you create a new response type, and if you want to reuse some existing plugins, you have to recreate some plugins for this type.

However, if your response object inherits from an existing response object (like jResponseHtml), you can use existing plugins in your template, unless you modify the value of the $type property of the response object.

See the documentation to create template plugins.