请求对象

The Request object encapsulates the HTTP request data from the current Environment object. Each Slim application has one Request object. You use the Request object to inspect the current HTTP request’s method, headers, and body. The Request object is registered as a Pimple service on the application instance. You can fetch the Request object like this:

  1. <?php
  2. $request = $app['request'];

请求方法

You can inspect the current HTTP request method with the Request object’s getMethod() method. This returns a string value equal to GET, POST, PUT, DELETE, HEAD, OPTIONS, or PATCH.

  1. <?php
  2. $method = $app['request']->getMethod();

GET Method

You can detect HTTP GET requests with the Request object’s isGet() method.

  1. <?php
  2. if ($app['request']->isGet() === true) {
  3. // Do something
  4. }

POST Method

You can detect HTTP POST requests with the Request object’s isPost() method.

  1. <?php
  2. if ($app['request']->isPost() === true) {
  3. // Do something
  4. }

PUT Method

You can detect HTTP PUT requests with the Request object’s isPut() method.

  1. <?php
  2. if ($app['request']->isPut() === true) {
  3. // Do something
  4. }

DELETE Method

You can detect HTTP DELETE requests with the Request object’s isDelete() method.

  1. <?php
  2. if ($app['request']->isDelete() === true) {
  3. // Do something
  4. }

HEAD Method

You can detect HTTP HEAD requests with the Request object’s isHead() method.

  1. <?php
  2. if ($app['request']->isHead() === true) {
  3. // Do something
  4. }

OPTIONS Method

You can detect HTTP OPTIONS requests with the Request object’s isOptions() method.

  1. <?php
  2. if ($app['request']->isOptions() === true) {
  3. // Do something
  4. }

PATCH Method

You can detect HTTP PATCH requests with the Request object’s isPatch() method.

  1. <?php
  2. if ($app['request']->isPatch() === true) {
  3. // Do something
  4. }

Method Override

There are two ways to override the HTTP request method. You can override the HTTP request method using the _METHOD parameter, or you can send a custom X-HTTP-Method-Override header. This is particularly useful if, for example, you need to mimic a PUT request using a traditional web browser that only supports POST requests. You can always fetch the original (non-overridden) HTTP method with the Request object’s getOriginalMethod() method.

With a body parameter

Include a _METHOD parameter in a POST request body. You must use the application/x-www-form-urlencoded content type.

With a header

You may also include the X-HTTP-Method-Override header in the HTTP request.

请求 URL

The Request object provides several methods to inspect the HTTP request URL.

Scheme

You can fetch the HTTP request scheme (e.g., HTTP or HTTPS) with the Request object’s getScheme() method.

  1. <?php
  2. $scheme = $app['request']->getScheme();

Host

You can fetch the HTTP request’s host (e.g., example.com) with the Request object’s getHost() method.

  1. <?php
  2. $host = $app['request']->getHost();

Port

You can fetch the HTTP request’s port number (e.g., 443) with the Request object’s getPort() method.

  1. <?php
  2. $port = $app['request']->getPort();

URL (physical path)

You can fetch the HTTP request’s physical directory path (relative to the document root) with the Request object’s getScriptName() method. This will be an empty string unless the Slim application is installed in a physical subdirectory beneath your document root.

  1. <?php
  2. $realPath = $app['request']->getScriptName();

URL (virtual path)

You can fetch the HTTP request’s virtual directory path with the Request object’s getPathInfo() method. This will return a string value that likely matches a Slim application route path.

  1. <?php
  2. $virtualPath = $app['request']->getPathInfo();

URL (physical + virtual paths)

You can fetch the complete physical and virtual path with the Request object’s getPath() method. This is useful if you must create an application link URL to an application resource to be rendered in an application template.

  1. <?php
  2. $fullPath = $app['request']->getPath();

Query String

You can fetch the HTTP request’s raw query string with the Request object’s getQueryString() method. You probably want to use the Request object’s get() method instead.

  1. <?php
  2. $queryString = $app['request']->getQueryString();

请求头部

There are several ways to inspect the Request object’s headers.

All Headers

You can fetch an associative array of all headers with the Request object’s getHeaders() method.

  1. <?php
  2. $headers = $app['request']->getHeaders();

Detect Header

You can test for the presence of a header with the Request object’s hasHeader($key) method.

  1. <?php
  2. if ($app['request']->hasHeader('Accept') === true) {
  3. // Do something
  4. }

Fetch Single Header

You can fetch a single header value with the Request object’s getHeader($key) method.

  1. <?php
  2. $headerValue = $app['request']->getHeader('Accept');

请求 Cookies

There are several ways to inspect the Request object’s cookies.

All Cookies

You can fetch an associative array of all cookies with the Request object’s getCookies() method.

  1. <?php
  2. $cookies = $app['request']->getCookies();

Detect Cookie

You can test for the presence of a cookie with the Request object’s hasCookie() method.

  1. <?php
  2. if ($app['request']->hasCookie('logged_in') === true) {
  3. // Do something
  4. }

Fetch Single Cookie

You can fetch a single cookie value with the Request object’s getCookie($key) method.

  1. <?php
  2. $cookieValue = $app['request']->getCookie('user_name');

请求主体

You can fetch the raw HTTP request body with the Request object’s getBody() method. Bear in mind this returns a string value containing the HTTP request body exactly as sent by the HTTP client.

  1. <?php
  2. $body = $app['request']->getBody();

This is useful if you need to fetch and decode a JSON request, for example. However, you’re probably more familiar with traditional HTML form submissions. Slim provides the following helper methods to inspect HTTP request parameters submitted via HTML forms.

GET data

You can inspect GET request parameters (e.g., query string data) with the Request object’s get() method. If you invoke the method without an argument, it returns an associative array for all GET request data. If you invoke the method with an argument, it returns a single string value for the specified parameter name.

  1. <?php
  2. // All GET data
  3. $data = $app['request']->get();
  4. // Single GET parameter
  5. $value = $app['request']->get('id');

POST & DELETE data

You can inspect POST request parameters with the Request object’s post() method. If you invoke the method without an argument, it returns an associative array for all POST request data. If you invoke the method with an argument, it returns a single string value for the specified parameter name.

  1. <?php
  2. // All POST data
  3. $data = $app['request']->post();
  4. // Single POST parameter
  5. $value = $app['request']->post('id');

The Request object also provides put() and delete() methods. These are aliases of the post() method.

The Request object’s post(), put(), and delete() methods work only if the HTTP request content type is application/x-www-form-urlencoded.

辅助方法

The Request object provides additional methods to inspect the HTTP request metadata (e.g., content type, charset, length, IP, and referrer).

Detect AJAX / XHR requests

You can detect AJAX/XHR requests with the Request object’s isAjax() and isXhr() methods. Both methods do the same thing, so choose only one. These methods detect the presence of the X-Requested-With HTTP request header and ensure its value is XMLHttpRequest. These methods also return true if the isajax parameter is provided in the HTTP request query string or body.

  1. <?php
  2. if ($app['request']->isAjax() === true) {
  3. // Do something
  4. }

Content Type

You can fetch the HTTP request content type with the Request object’s getContentType() method. This returns the Content-Type header’s full value as provided by the HTTP client.

  1. <?php
  2. $contentType = $app['request']->getContentType();

Media Type

You may not want the complete Content-Type header. What if, instead, you only want the media type? You can fetch the HTTP request media type with the Request object’s getMediaType() method.

  1. <?php
  2. $mediaType = $app['request']->getMediaType();

You can fetch the appended media type parameters as an associative array with the Request object’s getMediaTypeParams() method.

  1. <?php
  2. $mediaParams = $app['request']->getMediaTypeParams();

Character Set

One of the most common media type parameters is the HTTP request character set. The Request object provides a dedicated method to retrieve this media type parameter.

  1. <?php
  2. $charset = $app['request']->getContentCharset();

Content Length

You can fetch the HTTP request content length with the Request object’s getContentLength() method.

  1. <?php
  2. $length = $app['request']->getContentLength();

IP Address

You can fetch the HTTP request’s source IP address with the Request object’s getClientIp() method. This method respects values provided by the X-Forwarded-For and X-Client-IP headers if present.

  1. <?php
  2. $ip = $app['request']->getClientIp();

Referrer

You can fetch the HTTP request referrer (if present) with the Request object’s getReferrer() and getReferer() methods.

  1. <?php
  2. $ref = $app['request']->getReferer();

User Agent

You can fetch the HTTP user agent string with the Request object’s getUserAgent() method.

  1. <?php
  2. $agent = $app['request']->getUserAgent();