Request
Overview
Each Skinny application instance has one request object. The request object is an abstraction of the current HTTP request and allows you to easily interact with the Skinny application’s environment variables. Although each Skinny application includes a default request object, the \Skinny\Http\Request class is idempotent; you may instantiate the class at will (in middleware or elsewhere in your Skinny application) without affecting the application as a whole. You can obtain a reference to the Skinny application’s request object like this:
<?php
// Returns instance of \Skinny\Http\Request
$request = $app->request;
Method
Every HTTP request has a method (e.g. GET or POST). You can obtain the current HTTP request method via the Skinny application’s request object:
/**
* What is the request method?
* @return string (e.g. GET, POST, PUT, DELETE)
*/
$app->request->getMethod();
/**
* Is this a GET request?
* @return bool
*/
$app->request->isGet();
/**
* Is this a POST request?
* @return bool
*/
$app->request->isPost();
/**
* Is this a PUT request?
* @return bool
*/
$app->request->isPut();
/**
* Is this a DELETE request?
* @return bool
*/
$app->request->isDelete();
/**
* Is this a HEAD request?
* @return bool
*/
$app->request->isHead();
/**
* Is this a OPTIONS request?
* @return bool
*/
$app->request->isOptions();
/**
* Is this a PATCH request?
* @return bool
*/
$app->request->isPatch();
/**
* Is this a XHR/AJAX request?
* @return bool
*/
$app->request->isAjax();
Headers
A Skinny application will automatically parse all HTTP request headers. You can access the request headers using the request object’s public headers property. The headers property is an instance of \Skinny\Helper\Set, meaning it provides a simple, standardized interface to interactive with the HTTP request headers.
<?php
$app = \Skinny\Skinny::newInstance();
// Get request headers as associative array
$headers = $app->request->headers;
// Get the ACCEPT_CHARSET header
$charset = $app->request->headers->get('ACCEPT_CHARSET');
The HTTP specification states that HTTP header names may be uppercase, lowercase, or mixed-case. Skinny is smart enough to parse and return header values whether you request a header value using upper, lower, or mixed case header name, with either underscores or dashes. So use the naming convention with which you are most comfortable.
Body
Use the request object’s getBody() method to fetch the raw HTTP request body sent by the HTTP client. This is particularly useful for Skinny application’s that consume JSON or XML requests.
<?php
$app = \Skinny\Skinny::newInstance();
$body = $app->request->getBody();
Variables
An HTTP request may have associated variables (not to be confused with route variables). The GET, POST, or PUT variables sent with the current HTTP request are exposed via the Skinny application’s request object.
If you want to quickly fetch a request variable value without considering its type, use the request object’s params() method:
<?php
$app = \Skinny\Skinny::newInstance();
$paramValue = $app->request->params('paramName');
The params() method will first search PUT variables, then POST variables, then GET variables. If no variables are found, null is returned. If you only want to search for a specific type of variable, you can use these methods instead:
<?php
//GET variable
$paramValue = $app->request->get('paramName');
//POST variable
$paramValue = $app->request->post('paramName');
//PUT variable
$paramValue = $app->request->put('paramName');
If a variable does not exist, each method above will return null. You can also invoke any of these functions without an argument to obtain an array of all variables of the given type:
<?php
$allGetVars = $app->request->get();
$allPostVars = $app->request->post();
$allPutVars = $app->request->put();
Cookies
Get Cookies
A Skinny application will automatically parse cookies sent with the current HTTP request. You can fetch cookie values with the Skinny application’s getCookie() helper method like this:
<?php
$app = \Skinny\Skinny::newInstance();
$foo = $app->getCookie('foo');
Only Cookies sent with the current HTTP request are accessible with this method. If you set a cookie during the current request, it will not be accessible with this method until the subsequent request. Bear in mind the Skinny’s getCookie() method is a convenience. You may also retrieve the complete set of HTTP request cookies directly from the \Skinny\Http\Request object like this:
<?php
$cookies = $app->request->cookies;
This will return an instance of \Skinny\Helper\Set so you can use its simple, standardized interface to inspect the request’s cookies.
Cookie Encryption
You can optionally choose to encrypt all cookies stored on the HTTP client with the Skinny app’s cookies.encrypt setting. When this setting is true, all cookies will be encrypted using your designated secret key and cipher.
It’s really that easy. Cookies will be encrypted automatically before they are sent to the client. They will also be decrypted on-demand when you request them with \Skinny\Skinny::getCookie() during subsequent requests.
Paths
Every HTTP request received by a Skinny application will have a root URI and a resource URI.
Root URI
The root URI is the physical URL path of the directory in which the Skinny application is instantiated and run. If a Skinny application is instantiated in index.php within the top-most directory of the virtual host’s document root, the root URI will be an empty string. If a Skinny application is instantiated and run in index.php within a physical subdirectory of the virtual host’s document root, the root URI will be the path to that subdirectory with a leading slash and without a trailing slash.
Resource URI
The resource URI is the virtual URI path of an application resource. The resource URI will be matched to the Skinny application’s routes.
Assume the Skinny application is installed in a physical subdirectory /foo beneath your virtual host’s document root. Also assume the full HTTP request URL (what you’d see in the browser location bar) is /foo/books/1. The root URI is /foo (the path to the physical directory in which the Skinny application is instantiated) and the resource URI is /books/1 (the path to the application resource).
You can get the HTTP request’s root URI and resource URI with the request object’s getRootUri() and getResourceUri() methods:
<?php
$app = \Skinny\Skinny::newInstance();
// Get request object
$req = $app->request;
//Get root URI
$rootUri = $req->getRootUri();
//Get resource URI
$resourceUri = $req->getResourceUri();
XHR
When using a Javascript framework like MooTools or jQuery to execute an XMLHttpRequest, the XMLHttpRequest will usually be sent with a X-Requested-With HTTP header. The Skinny application will detect the HTTP request’s X-Requested-With header and flag the request as such. If for some reason an XMLHttpRequest cannot be sent with the X-Requested-With HTTP header, you can force the Skinny application to assume an HTTP request is an XMLHttpRequest by setting a GET, POST, or PUT parameter in the HTTP request named “isajax” with a truthy value.
Use the request object’s isAjax() or isXhr() method to tell if the current request is an XHR/Ajax request:
<?php
$isXHR = $app->request->isAjax();
$isXHR = $app->request->isXhr();
Helpers
The Skinny application’s request object provides several helper methods to fetch common HTTP request information:
Content Type
Fetch the request’s content type (e.g. “application/json;charset=utf-8”):
<?php
$req = $app->request;
$req->getContentType();
Media Type
Fetch the request’s media type (e.g. “application/json”):
<?php
$req = $app->request;
$req->getMediaType();
Media Type Params
Fetch the request’s media type parameters (e.g. [charset => “utf-8”]):
<?php
$req = $app->request;
$req->getMediaTypeParams();
Content Charset
Fetch the request’s content character set (e.g. “utf-8”):
<?php
$req = $app->request;
$req->getContentCharset();
Content Length
Fetch the request’s content length:
<?php
$req = $app->request;
$req->getContentLength();
Host
Fetch the request’s host (e.g. “somedomain.com”):
<?php
$req = $app->request;
$req->getHost();
Host with Port
Fetch the request’s host with port (e.g. “somedomain.com:80”):
<?php
$req = $app->request;
$req->getHostWithPort();
Port
Fetch the request’s port (e.g. 80):
<?php
$req = $app->request;
$req->getPort();
Scheme
Fetch the request’s scheme (e.g. “http” or “https”):
<?php
$req = $app->request;
$req->getScheme();
Path
Fetch the request’s path (root URI + resource URI):
<?php
$req = $app->request;
$req->getPath();
URL
Fetch the request’s URL (scheme + host [ + port if non-standard ]):
<?php
$req = $app->request;
$req->getUrl();
IP Address
Fetch the request’s IP address:
<?php
$req = $app->request;
$req->getIp();
Referer
Fetch the request’s referrer:
<?php
$req = $app->request;
$req->getReferrer();
User Agent
Fetch the request’s user agent string:
<?php
$req = $app->request;
$req->getUserAgent();
Content Types
By default Skinny does not parse any other content-type other than the standard form data because PHP does not support it. That means if you attempt to post application/json you will not be able to access the data via $app->request->post();
To solve this You must parse the content type yourself. You can either do this on a per-route basis
//For application/json
$data = json_decode($app->request->getBody());
or use middleware like \Skinny\Middleware\ContentTypes