Overview

The Slim Framework implements a derivation of the Rack protocol. Whenyou instantiate a Slim application, it immediately inspects the $_SERVER superglobal and derives a set of environmentvariables that dictate application behavior.

What is the Environment?

A Slim application’s “environment” is an associative array of settings that are parsed once and made accessible tothe Slim application and its middleware. You are free to modify the environment variables during runtime; changeswill propagate immediately throughout the application.

When you instantiate a Slim application, the environment variables are derived from the $_SERVER superglobal; you donot need to set these yourself. However, you are free to modify or supplement these variables in Slim middleware.

These variables are fundamental to determining how your Slim application runs: the resource URI, the HTTP method,the HTTP request body, the URL query parameters, error output, and more. Middleware, described later, gives youthe power to - among other things - manipulate environment variables before and/or after the Slim application is run.

Environment Variables

The following text respectfully borrows the same information originally available athttp://rack.rubyforge.org/doc/files/SPEC.html. The environment array must include these variables:

  • REQUEST_METHOD
  • The HTTP request method. This is required and may never be an empty string.
  • SCRIPT_NAME
  • The initial portion of the request URI’s “path” that corresponds to the physical directory in which the Slimapplication is installed — so that the application knows its virtual “location”. This may be an empty string if theapplication is installed in the top-level of the public document root directory. This will never have a trailing slash.
  • PATH_INFO
  • The remaining portion of the request URI’s “path” that determines the “virtual” location of the HTTP request’s target resource within the Slim application context. This will always have a leading slash; it may or may not have a trailing slash.
  • QUERY_STRING
  • The part of the HTTP request’s URI after, but not including, the “?”. This is required but may be an empty string.
  • SERVER_NAME
  • When combined with SCRIPT_NAME and PATH_INFO, this can be used to create a fully qualified URL to an application resource. However, if HTTP_HOST is present, that should be used instead of this. This is required and may never be an empty string.
  • SERVER_PORT
  • When combined with SCRIPT_NAME and PATH_INFO, this can be used to create a fully qualified URL to any application resource. This is required and may never be an empty string.
  • HTTP_*
  • Variables matching the HTTP request headers sent by the client. The existence of these variables correspond with those sent in the current HTTP request.
  • slim.url_scheme
  • Will be “http” or “https” depending on the HTTP request URL.
  • slim.input
  • Will be a string representing the raw HTTP request body. If the HTTP request body is empty (e.g. with a GET request), this will be an empty string.
  • slim.errors
  • Must always be a writable resource; by default, this is a write-only resource handle to php://stderr.The Slim application can store its own data in the environment, too. The environment array’s keys must contain at leastone dot, and should be prefixed uniquely (e.g. “prefix.foo”). The prefix slim. is reserved for use by Slim itselfand must not be used otherwise. The environment must not contain the keys HTTPCONTENT_TYPE or HTTP_CONTENT_LENGTH(use the versions without HTTP). The CGI keys (named without a period) must have String values. There are thefollowing restrictions:

  • slim.url_scheme must either be “http” or “https”.

  • slim.input must be a string.
  • There must be a valid, writable resource in slim.errors.
  • The REQUEST_METHOD must be a valid token.
  • The SCRIPT_NAME, if non-empty, must start with “/”
  • The PATH_INFO, if non-empty, must start with “/”
  • The CONTENT_LENGTH, if given, must consist of digits only.
  • One of SCRIPT_NAME or PATH_INFO must be set. PATH_INFO should be “/” if SCRIPT_NAME is empty. SCRIPT_NAMEnever should be “/”, but instead be an empty string.