Laravel Request

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:-

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.

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.

Example:-

The “fullUrl” method returns full url including query string.

Example:-

The “fullUrlWithQuery” method return full url and appends the query string based on the arguments passed to it.

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.

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:-

It returns value for input variable specified in method argument.

Example 2:-

It returns “John” as default value for “student_name” if it is not present in request.

Example 3:-

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.

“all” Method

The “all” method is used to retrieve all the input data available in the current request.

“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.

“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.

 

 

 

In this tutorial we have learn about the Laravel Request and its application with practical example. I hope you will like this tutorial.