Environment variables

Contents...

Webasyst framework offers several service functions to developers, which facilitate access to environment variables (arrays $_GET, $_POST, $_COOKIE, and $_SERVER). All functions used for the processing environment variables are accessible as the methods of static system class waRequest.

Method waRequest::getMethod() returns the type of the received HTTP request (GET or POST).

Environment variable reading methods:

waRequest::get($name, $default, $type)
waRequest::post($name, $default, $type)
waRequest::cookie($name, $default, $type)
waRequest::server($name, $default, $type)

In addition to the methods used for accessing standard environment variables, class waRequest also offers a similar method to access parameters received from the frontend request routing system (i.e. request parameters contained in SEO-friendly URLs):

waRequest::param($name, $default, $type)

All these methods have a common calling signature with three optional arguments $name, $default, $type:

  • $name is the parameter/variable name
  • $default is the default value (which is returned if the requested variable is missing)
  • $type specifies the data type to which the received (string-type) value must be converted

Supported data types for variables:

  • 'int': conversion to the integer numerical type
  • 'array_int': if an array is received, all its elements are converted to the integer type; if a string of the form '1,3,6,10' is received, then an array of the corresponding integer values is returned
  • 'string': conversion to the string type (default value)
  • 'string_trim': the string contained in a variable is additionally processed by the trim function

Examples

  • waRequest::get() returns the entire $_GET array
  • $id = waRequest::get('id'); returns $_GET['id'], or null if request parameter id is missing
  • $page = waRequest::get('p', 1); is similar to the previous example but, if the $_GET array does not contain the p element, then the specified default value will be returned, i.e. 1
  • $page = waRequest::get('p', 1, 'int'); is similar to the previous example but the returned result is additionally converted to the int type
  • $ids = waRequest::get('ids', array(), 'array_int'); returns the array of integers, or an empty array if ids array is not received
  • $name = waRequest::post('name', '', 'string_trim'); returns parameter name from the $_POST array without space-like characters at the beginning and at the end of string, or an empty string if no value is received