Quick links: Content - sections - sub sections
EN FR

Forget $_GET and $_POST variables

In an action of a controller, one of task you'll do very often, is to retrieve HTTP parameters.

In PHP, you usually get them into $_GET, $_POST, $_REQUEST, $_FILES etc.

However, except for files uploading ($_FILES), you'll never use them. You must call one of the methods of jController.

Your module used to be reusable, so it doesn't know how to retrieve parameters. For instance, depending of how URLS are configured (router), perhaps one of parameters is inside the pathinfo or in $_GET. A form could be changed, so parameters are in $_GET instead of $_POST. More important, HTTP parameters could be changed by a plugin, without changing original PHP arrays.

With other protocols like SOAP or XML-RPC, parameters can be stored into the body of the request. The Request object of jelix extract them for you.

This is why you have one unified way to retrieve a parameter: the method param() of a controller. This is the same method for GET, POST, PUT, DELETE, SOAP, XML-RPC, JSON etc...

Accessing client-request parameters

HTTP client-request parameters are accessible through an instance of jRequest, which is accessible from jCoordinator. For example :


 $id = jApp::coord()->request->getParam('id');

Although jController provides the method param():


  $id = $this->param('id');

Fetching an undefined parameter makes param() to return null or it's second argument for example:


   $title = $this->param('title','Hello World (default title)');

jController provides a few filtering methods like intParam(), floatParam() or boolParam() which use the same arguments:

  • intParam() to fetch an integer,
  • floatParam() to fetch a float value,
  • boolParam() returns false except if the parameter's string value is "true", "1", "on", or "yes"

Use jFilter for any other type of filtering.

Case of the PUT http method

PHP doesn't support correctly the PUT HTTP method. Indeed it does not retrieve parameters which are in the body of the request, like it does for POST (there isn't a $_PUT variable).

Jelix extract itself parameters (via php://input), if the content type is application/x-www-url-encoded or multipart/form-data like POST. You'll call the param() method in your controller like for other HTTP methods.

PHP does not put uploaded files (that are in the body of the request for multipart/form-data content type) into $_FILES for the PUT method. You'll have to retrieve and process them your self by getting it with the param() method.

Retrieving JSON content

When the content type of the request is application/json, Jelix decode the content, and if is a JSON object, it is parsed as an associated array. So the object members will be accessible like any parameters with the param() method.

For example, Jelix receives this JSON object:


{
    "firstname": "tom",
    "lastname": "dupont"
}

Then you will access to each property of the object by doing:


$firstname = $this->param('firstname');
$lastname = $this->param('lastname');

If you prefer retrieving the JSON content, call the method getBody() of the request object:


    $values = json_decode($this->request->getBody());

Retrieving specific content

If the content type of the request is not application/x-www-url-encoded, multipart/form-data or application/json, you should retrieve the content with one of these solution:

  1. By retrieving the __httpbody parameter:

  $data = $this->param('__httpbody');
  1. by calling the method getBody() on the request object:

    $data = json_decode($this->request->getBody());

Then you can process the content. You'll find the content type into $_SERVER["CONTENT_TYPE"].