Getting started
Routing and controllers
Command line
Databases (SQL)
Databases (NoSQL)
Security
Packages
Learn more
- Array helper
- Caching
- Command bus
- Date and time
- Events
- File system
- HTML helper
- Humanizer
- Image manipulation
- Internationalization
- Logging
- Number helper
- Pagination
- 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 php$_GET, php$_POST, php$_FILES, php$_COOKIE and php$_SERVER as well as easy access to request data and other useful request information.
An instance of the request class is always available in all controller classes. It is also easily made available in route closures.
Accessing data
The phpget method allows you to access query string data.
// Get all query string data (returns an empty array if there is no data)
$all = $this->request->get();
// Get data from a specific key (will return NULL if the key doesn't exist)
$foo = $this->request->get('foo');
// Get data from a specific key and return FASLE if the key doesn't exist
$foo = $this->request->get('foo', false);
The phppost method allows you to access phpPOST data.
// Get all POST data (returns an empty array if there is no data)
$all = $this->request->post();
// Get data from a specific key (will return NULL if the key doesn't exist)
$foo = $this->request->post('foo');
// Get data from a specific key and return FASLE if the key doesn't exist
$foo = $this->request->post('foo', false);
The phpput method allows you to access phpPUT data.
The method expects the request body to be one of the following types;
phpapplication/x-www-form-urlencoded,phptext/jsonorphpapplication/json. If you want to access the raw request body then you'll have to use thephpbodymethod instead.
// Get all PUT data (returns an empty array if there is no data)
$all = $this->request->put();
// Get data from a specific key (will return NULL if the key doesn't exist)
$foo = $this->request->put('foo');
// Get data from a specific key and return FASLE if the key doesn't exist
$foo = $this->request->put('foo', false);
The phppatch method allows you to access phpPATCH data.
The method expects the request body to be one of the following types;
phpapplication/x-www-form-urlencoded,phptext/jsonorphpapplication/json. If you want to access the raw request body then you'll have to use thephpbodymethod instead.
// Get all PATCH data (returns an empty array if there is no data)
$all = $this->request->patch();
// Get data from a specific key (will return NULL if the key doesn't exist)
$foo = $this->request->patch('foo');
// Get data from a specific key and return FASLE if the key doesn't exist
$foo = $this->request->patch('foo', false);
The phpdelete method allows you to access phpDELETE data.
The method expects the request body to be one of the following types;
phpapplication/x-www-form-urlencoded,phptext/jsonorphpapplication/json. If you want to access the raw request body then you'll have to use thephpbodymethod instead.
// Get all DELETE data (returns an empty array if there is no data)
$all = $this->request->delete();
// Get data from a specific key (will return NULL if the key doesn't exist)
$foo = $this->request->delete('foo');
// Get data from a specific key and return FASLE if the key doesn't exist
$foo = $this->request->delete('foo', false);
The phpbody method allows you to access the raw request body.
$body = $this->request->body();
The phpbodyAsStream 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 file uploads via PUT requests.
$body = $this->request->bodyAsStream();
$storageStream = fopen($this->app->getPath() . '/storage/uploads/' . $fileName, 'w');
$filesize = stream_copy_to_stream($body, $storageStream);
The phpdata method allows you to access data from the current request method.
// Get all request data (returns an empty array if there is no data)
$all = $this->request->data();
// Get data from a specific key (will return NULL if the key doesn't exist)
$foo = $this->request->data('foo');
// Get data from a specific key and return FASLE if the key doesn't exist
$foo = $this->request->data('foo', false);
The phphas method lets you check if a data key exist.
$exists = $this->request->has('foo');
The phpwhitelisted methods returns all request data where keys not in the whitelist have been removed.
// Returns input data where everything except 'foo' and 'bar' keys have been removed
$whitelisted = $this->request->whitelisted(['foo', 'bar']);
// You can also specify default values that will be used if the keys don't exist
$whitelisted = $this->request->whitelisted(['foo', 'bar'], ['foo' => 'baz', 'bar' => 'bax']);
The phpblacklisted methods returns all request data where keys in the blacklist have been removed.
// Returns input data where the 'foo' and 'bar' keys have been removed
$blacklisted = $this->request->blacklisted(['foo', 'bar']);
// You can also specify default values that will be used if the keys don't exist
$blacklisted = $this->request->blacklisted(['foo', 'bar'], ['baz' => 'foo', 'bax' => 'bar']);
The phpfile method allows you to access files that have been upload via a POST request.
// Get all uploaded files (returns an empty array if there is no data)
$files = $this->request->file();
// Get a specific file or an array of files if it's a multi-upload.
$file = $this->request->file('myfile');
// If you want to get a specific file from a multi-upload then you'll have to add the array key as well
$files = $this->request->file('myfile.0');
Uploaded files are represented as a phpUploadedFile object. The class extends the phpSplFileInfo class with the following methods.
| Method name | Description |
|---|---|
| getName | Returns the name of the file that was uploaded. |
| getReportedSize | Returns the size reported by the client. |
| getReportedType | 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 | Moves the file to the specified storage location. |
The values reported by the client should not be trusted. Use the
phpgetSizemethod to retrieve the actual filesize.
The phpserver method lets you access server data.
// Get all server data (returns an empty array if there is no data)
$all = $this->request->server();
// Get data from a specific key (will return NULL if the key doesn't exist)
$foo = $this->request->server('SERVER_PORT');
// Get data from a specific key and return '80' if the key doesn't exist
$foo = $this->request->server('SERVER_PORT', 80);
Reading cookies
The phpcookie method will return the value of the selected cookie.
// Returns the value of the chosen cookie (returns NULL if the cookie doesn't exist)
$value = $this->request->cookie('chocolate');
// Return the value of the chosen cookie and return FASLE if the cookie doesn't exist
$value = $this->request->cookie('chocolate', false);
The phpsignedCookie method will return the value of the selected signed cookie.
// Returns the value of the chosen cookie (returns NULL if the cookie doesn't exist)
$value = $this->request->signedCookie('chocolate');
// Return the value of the chosen cookie and return FASLE if the cookie doesn't exist
$value = $this->request->signedCookie('chocolate', false);
The benefit of using signed cookies over regular cookies is that their values can not be tampered with on the client site.
Setting regular and signed cookies is done using the response class.
Request information
The phpheader method returns the value of the chosen request header.
// Returns the value of the chosen header (returns NULL if the header doesn't exist)
$value = $this->request->header('content-type');
// Returns the value of the chosen header and returns 'text/plain' if the header doesn't exist
$value = $this->request->header('content-type', 'text/plain');
The phpreferer method returns the address that referred the client to the current resource. NULL is returned if there is no referrer.
// Returns the referrer if there is one and NULL if not
$referer = $this->request->referer();
// Returns the referrer if there is one and the URL to the 'home' route if there isn't one
$referer = $this->request->referer($this->urlBuilder->toRoute('home'));
The phpacceptableContentTypes returns an array of acceptable content types in descending order of preference.
$acceptableContentTypes = $this->request->acceptableContentTypes();
The phpacceptableLanguages returns an array of acceptable languages in descending order of preference.
$acceptableLanguages = $this->request->acceptableLanguages();
The phpacceptableCharsets returns an array of acceptable character sets in descending order of preference.
$acceptableCharsets = $this->request->acceptableCharsets();
The phpacceptableEncodings returns an array of acceptable encodings sets in descending order of preference.
$acceptableEncodings = $this->request->acceptableEncodings();
The phpip method returns IP of the client that made the request.
$ip = $this->request->ip();
The phpisAjax method returns TRUE if the request was made using AJAX and FALSE if not.
$isAjax = $this->request->isAjax();
The phpisSecure method returns TRUE if the request was made using HTTPS and FALSE if not.
$isSecure = $this->request->isSecure();
The phpisSafe method returns TRUE if the request method used is considered safe and FALSE if not. The methods that are considered safe are phpGET and phpHEAD.
$isSafe = $this->request->isSafe();
The phpbaseURL method returns the base URL of the request.
// A request to 'http://example.org/foo/bar' will return 'http://example.org'
$baseURL = $this->request->baseURL();
The phppath method will return the request path.
// A request to 'http://example.org/foo/bar' will return '/foo/bar'
$path = $this->request->path();
The phplanguage method returns the request language (see the phpapp/config/application.php config file for more information).
$language = $this->request->language();
The phplanguagePrefix method returns the language prefix (see the phpapp/config/application.php config file for more information).
$languagePrefix = $this->request->languagePrefix();
The phpmethod method returns the request method used to access the resource.
$method = $this->request->method();
The phprealMethod method returns the real request method used to access the resource.
$method = $this->request->realMethod();
The phpisFaked method returns TRUE if the request method method has been faked and FALSE if not.
$isFaked = $this->request->isFaked();
The phpusername method returns a basic HTTP authentication username of NULL if it isn't set.
$username = $this->request->username();
The phppassword method returns a basic HTTP authentication password of NULL if it isn't set.
$password = $this->request->password();
The phpgetRoute method returns the route that matched the request.
$route = $this->request->getRoute();