Helpers

The Slim 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”):

  1. <?php
  2. $req = $app->request;
  3. $req->getContentType();

Media Type

Fetch the request’s media type (e.g. “application/json”):

  1. <?php
  2. $req = $app->request;
  3. $req->getMediaType();

Media Type Params

Fetch the request’s media type parameters (e.g. [charset => “utf-8”]):

  1. <?php
  2. $req = $app->request;
  3. $req->getMediaTypeParams();

Content Charset

Fetch the request’s content character set (e.g. “utf-8”):

  1. <?php
  2. $req = $app->request;
  3. $req->getContentCharset();

Content Length

Fetch the request’s content length:

  1. <?php
  2. $req = $app->request;
  3. $req->getContentLength();

Host

Fetch the request’s host (e.g. “slimframework.com”):

  1. <?php
  2. $req = $app->request;
  3. $req->getHost();

Host with Port

Fetch the request’s host with port (e.g. “slimframework.com:80”):

  1. <?php
  2. $req = $app->request;
  3. $req->getHostWithPort();

Port

Fetch the request’s port (e.g. 80):

  1. <?php
  2. $req = $app->request;
  3. $req->getPort();

Scheme

Fetch the request’s scheme (e.g. “http” or “https”):

  1. <?php
  2. $req = $app->request;
  3. $req->getScheme();

Path

Fetch the request’s path (root URI + resource URI):

  1. <?php
  2. $req = $app->request;
  3. $req->getPath();

URL

Fetch the request’s URL (scheme + host [ + port if non-standard ]):

  1. <?php
  2. $req = $app->request;
  3. $req->getUrl();

IP Address

Fetch the request’s IP address:

  1. <?php
  2. $req = $app->request;
  3. $req->getIp();

Referer

Fetch the request’s referrer:

  1. <?php
  2. $req = $app->request;
  3. $req->getReferrer();

User Agent

Fetch the request’s user agent string:

  1. <?php
  2. $req = $app->request;
  3. $req->getUserAgent();