The Environment object encapsulates the $_SERVER superglobal array and decouples the Slim application from the PHP global environment. Decoupling the Slim application from the PHP global environment lets us create HTTP requests that may (or may not) resemble the global environment. This is particuarly useful for unit testing and initiating sub-requests. You can fetch the current Environment object anywhere in your Slim application like this:
Each Slim application has an Environment object with various properties that determine application behavior. Many of these properties mirror those found in the $_SERVER superglobal array. Some properties are required. Other properties are optional.
The HTTP request method. This must be one of “GET”, “POST”, “PUT”, “DELETE”, “HEAD”, “PATCH”, or “OPTIONS”.
The absolute path name to the front-controller PHP script relative to your document root, disregarding any URL rewriting performed by your web server.
The absolute path name of the HTTP request URI, including any URL rewriting changes performed by your web server.
The part of the HTTP request’s URI path after, but not including, the “?”. This may be an empty string if the current HTTP request does not specify a query string.
The name of the server host under which the current script is executing. If the script is running on a virtual host, this will be the value defined for that virtual host.
The port on the server machine being used by the web server for communication. For default setups, this will be ‘80’; using SSL, for instance, will change this to whatever your defined secure HTTP port is.
Set to a non-empty value if the script was queried through the HTTPS protocol.
The HTTP request content type (e.g., application/json;charset=utf8)
The HTTP request content length. This must be an integer if present.
The HTTP request headers sent by the client. These values are identical to their counterparts in the $_SERVER superglobal array. If present, these values must retain the “HTTP_” prefix.
The HTTP Authentication header’s decoded username.
The HTTP Authentication header’s decoded password.
The raw HTTP Authentication header as sent by the HTTP client.
The HTTP Authentication header’s authentication type (e.g., “Basic” or “Digest”).
Each Slim application instantiates an Environment object using information from the current global environment. However, you may also create mock environment objects with custom information. Mock Environment objects are only useful when writing unit tests.