Getting started
Routing and controllers
Command line
Databases (SQL)
Databases (NoSQL)
Security
Packages
Learn more
- Array helper
- Caching
- Collections
- Command, event and query buses
- Date and time
- File system
- HTML helper
- Humanizer
- Image manipulation
- Internationalization
- Logging
- Number helper
- Pagination
- Retry helper
- Sessions
- String helper
- URL builder
- UUID helper
- Validation
- Views
Official packages
Request
The request class provides an object oriented interface to global variables such as $_GET
, $_POST
, $_FILES
, $_COOKIE
and $_SERVER
as well as easy access to other useful request information.
Accessing data
Query string, post data and parsed body
The getQuery
method returns a parameter collection containing query string data.
$queryString = $this->request->getQuery();
// You can also use the read-only "query" property
$queryString = $this->request->query;
The get
method of the parameter collection returns the value of a parameter and null
if it doesn't exist.
$value = $queryString->get('foo');
// You can also specify a custom default return value
$value = $queryString->get('foo', false);
The getPost
method returns a parameter collection containing post data.
// Get all post data
$post = $this->request->getPost();
// You can also use the read-only "post" property
$post = $this->request->post;
The getBody
method returns a parameter collection containing data from a parsed request body. The method currently supports application/x-www-form-urlencoded
and application/json
request bodies.
$body = $this->request->getBody();
The getData
method returns a parameter collection containing the data corresponding to the request method (query string for GET
requests, post data or a parsed body for POST
requests and the parsed body for any other type of request).
$data = $this->request->getData();
The parameter collections also includes the following methods in addition to the ones shown in the examples above.
Method | Description |
---|---|
all() | Returns an array containing all the parameters |
add($name, $value) | Adds a parameter |
has($name) | Returns true if the collection has the parameter and false if not |
remove($name) | Removes a parameter |
whitelisted($keys, $defaults = []) | Returns a whitelisted array of parameters |
blacklisted($keys, $defaults = []) | Returns an array of parameters where the blacklisted keys have been removed |
Raw request body
The getRawBody()
method returns the raw request body as a string.
$body = $this->request->getRawBody();
The getRawBodyAsStream
method returns the raw request body as a stream. This can be useful if you want to minimize the memory footprint of your application while handling large request bodies.
$body = $this->request->getRawBodyAsStream();
$storageStream = fopen($this->app->getPath() . '/storage/uploads/' . $filename, 'w');
$fileSize = stream_copy_to_stream($body, $storageStream);
Cookies
The getCookies
method returns a cookie collection that allows to you access both signed and unsigned cookies.
$cookies = $this->request->getCookies();
// You can also use the read-only "cookies" property
$cookies = $this->request->cookies;
The get
method of the cookie collection returns the value of a cookie and null
if it doesn't exist.
$value = $cookies->get('foobar');
// You can also define a custom default return value
$value = $cookies->get('foobar', false);
If you want to read a signed cookie then you'll have to use the
getSigned
method. The benefit of using signed cookies is that they can't be tampered with on the client side.
The cookie collection also includes the following methods in addition to the ones shown in the examples above.
Method | Description |
---|---|
all() | Returns an array containing all the cookies |
add($name, $value) | Adds a cookie |
addSigned($name, $value) | Adds a signed cookie |
has($name) | Returns true if the cookie exists and false if not |
remove($name) | Removes a cookie |
Headers
The getHeaders
method returns a header collection.
$headers = $this->request->getHeaders();
// You can also use the read-only "headers" property
$headers = $this->request->headers;
The get
method of the header collection returns the value of the header an null
it it isn't set.
$value = $headers->get('content-type');
// You can also define a custom default return value
$value = $headers->get('content-type', 'text/plain');
The getAcceptableContentTypes
method of the header collection returns an array of acceptable content types in descending order of preference.
$contentTypes = $headers->getAcceptableContentTypes();
The getAcceptableLanguages
method of the header collection returns an array of acceptable languages in descending order of preference.
$languages = $headers->getAcceptableLanguages();
The getAcceptableCharsets
method of the header collection returns an array of acceptable character sets in descending order of preference.
$charsets = $headers->getAcceptableCharsets();
The getAcceptableEncodings
method of the header collection returns an array of acceptable encodings in descending order of preference.
$charsets = $headers->getAcceptableEncodings();
The header collection also includes the following methods in addition to the ones shown in the examples above.
Method | Description |
---|---|
all() | Returns an array containing all the headers |
add($name, $value) | Adds a header |
has($name) | Returns true if the header exists and false if not |
remove($name) | Removes a header |
getBearerToken() | Returns the bearer token or null if there isn't one |
Files
The getFiles
method returns a file collection that extends the parameter collection.
$files = $this->request->getFiles();
// You can also use the read-only "files" property
$files = $this->request->files;
The get
method of the file collection returns a UploadedFile
instance or null
if the file doesn't exist.
$file = $files->get('myfile');
// If you're fetching a file from a multi upload then you'll have to include the key as well
$file = $files->get('myfile.0');
The UploadedFile
class extends the FileInfo
class with the following methods.
Method | Description |
---|---|
getReportedFilename() | Returns the name of the file that was uploaded |
getReportedSize() | Returns the size reported by the client |
getReportedMimeType() | Returns the mime type reported by the client |
hasError() | Returns true if there is an error with the uploaded file and false if not |
getErrorCode() | Returns the error code associated with the error |
getErrorMessage() | Returns a human friendly error message |
isUploaded() | Returns true if the file is an actual upload and false if not |
moveTo($path) | Moves the file to the specified storage location |
The mime type and size values reported by the client should not be trusted (e.g. use the
getSize
method to retrieve the actual file size).
Server information
The getServer
method returns a collection that extends the parameter collection.
$server = $this->request->getServer();
// You can also use the read-only "server" property
$server = $this->request->server;
Request information
The getContentType
method returns the content type of the request body.
$contentType = $this->request->getContentType();
The getReferrer
method returns the address that referred the client to the current resource or null
if there is no referrer.
// Returns the referrer if there is one and "null" if not
$referrer = $this->request->getReferrer();
// Returns the referrer if there is one and the URL to the 'home' route if there isn't one
$referrer = $this->request->getReferrer($this->urlBuilder->toRoute('home'));
The getIp
method returns IP of the client that made the request.
$ip = $this->request->getIp();
The isAjax
method returns true
if the request was made using AJAX and false
if not.
$isAjax = $this->request->isAjax();
The isSecure
method returns true
if the request was made using HTTPS and false
if not.
$isSecure = $this->request->isSecure();
The isSafe
method returns true
if the request method used is considered safe and false
if not. The methods that are considered safe are GET
, HEAD
, OPTIONS
and TRACE
.
$isSafe = $this->request->isSafe();
The isCacheable
method returns true
if the request method used is considered cacheable and false
if not. The methods that are considered cacheable are GET
and HEAD
.
$isCacheable = $this->request->isCacheable();
The isIdempotent
method returns true
if the request method used is considered idempotent and false
if not. The methods that are considered idempotent are DELETE
, GET
, HEAD
, OPTIONS
, PUT
and TRACE
.
$isIdempotent = $this->request->isIdempotent();
The getBasePath
method returns the base path of the request. A request to http://example.org/foo/index.php
will return /foo
while a request to http://example.org/index.php
will return an empty string.
$basePath = $this->request->getBasePath();
The getBaseURL
method returns the base URL of the request. A request to http://example.org/foo/bar
will return http://example.org
.
$baseURL = $this->request->getBaseURL();
The getPath
method will return the request path. A request to http://example.org/index.php/foo/bar
or http://example.org/foo/bar
will return /foo/bar
.
$path = $this->request->getPath();
The getLanguage
method returns the request language (see the app/config/application.php
config file for more information).
$language = $this->request->getLanguage();
The getLanguagePrefix
method returns the language prefix (see the app/config/application.php
config file for more information).
$languagePrefix = $this->request->getLanguagePrefix();
The getMethod
method returns the request method used to access the resource.
$method = $this->request->getMethod();
The getRealMethod
method returns the real request method used to access the resource.
$method = $this->request->getRealMethod();
The isFaked
method returns true
if the request method method has been faked and false
if not.
$isFaked = $this->request->isFaked();
The getUsername
method returns a basic HTTP authentication username of null
if it isn't set.
$username = $this->request->getUsername();
The getPassword
method returns a basic HTTP authentication password of null
if it isn't set.
$password = $this->request->getPassword();
The getRoute
method returns the route that matched the request.
$route = $this->request->getRoute();