In this tutorial you will learn about the Laravel Request and its application with practical example.
Retrieving Request Information
In Laravel, by instantiating Illuminate\Http\Request we can retrive information about the current HTTP request. We can instantiate Illuminate\Http\Request via dependency injection. The Illuminate\Http\Request instance provides us a rich set of methods to get information about the current HTTP request.
Request URI
Request URI can be retrieved using the “path” method.
Example:-
1 |
$uri = $request->path(); |
So, if the full URL is http://xyz.com/foo/bar, the path method will return foo/bar
Example:-
The “is” method is used to retrieve the requested URI that matches the specified pattern.
1 2 3 |
if ($request->is('foo/*')) { // } |
It matches the specified URL pattern passed as method argument, in the above code snippet (*) is used as a wildcard.
Example:-
The “url” method returns full url ignoring query string.
1 |
$url = $request->url(); |
Example:-
The “fullUrl” method returns full url including query string.
1 |
$url = $request->fullUrl(); |
Example:-
The “fullUrlWithQuery” method return full url and appends the query string based on the arguments passed to it.
1 |
$url = $request->fullUrlWithQuery(['arg' => 'val']); |
So, if the full url is http://xyz.com/foo the following method will return http://xyz.com/foo?arg=val. Arguments must be passed in key value pair as above.
Request Method
The “method” method is used to retrieve HTTP verb or Request Method for the current request, and “isMethod “ method is used to check HTTP verb for the current request.
1 2 3 4 5 6 7 |
// returns the HTTP verb for the current HTTP Request $method = $request->method(); // returns true if the current HTTP Request method is "POST" // otherwise it returns false if ($request->isMethod('post')) { // } |
Retrieving Input
In Laravel, input values can also be easily retrieved using the same Illuminate\Http\Request instance. You don’t need to care about the request method(“get” or “post”) used to send the input. Same method is used to retrieve input values from both the request methods.
The Illuminate\Http\Request instance offers you following methods for accessing input values.
“input” Method
Example 1:-
1 |
$request->input('student_name'); |
It returns value for input variable specified in method argument.
Example 2:-
1 |
$request->input('student_name', 'john'); |
It returns “John” as default value for “student_name” if it is not present in request.
Example 3:-
1 |
$request->input('student.name'); |
The “input” method can also be used to retrieve JSON inputs, and “dot” is used to access individual elements inside JSON array.
“has” Method
The “has” method is used to determine the presence of the specified value in the request, it returns true if the specified value is present and is not an empty string.
1 2 3 |
if ($request->has('student.name')) { // } |
“all” Method
The “all” method is used to retrieve all the input data available in the current request.
1 |
$request->all(); |
“only” Method
The “only” method is used to retrieve selected input data from the request, it accepts array or list of input elements for which you want to retrieve value.
1 2 3 |
$request->only(['name', 'email']); or $request->only('name', 'email'); |
“except” Method
The “except” method is used to retrieve selected input data from the request same as “only” method, but it accepts array or list of input elements that you want to ignore.
Retrieving Input as Property
In Laravel, it is also possible to retrieve input data as property of Illuminate\Http\Request instance.
1 |
$request->student_name; |