Laravel 5: HTTP Layer – Requests Methods All

Laravel 5: HTTP Layer – Requests Methods All


All request methods listed in Laravel API: Illuminate/Http

 Actually not all of them, but great many.
All methods can be found in directory:

Once we injected Request like so:

… we can start using tools, by chaining them to $request object.

 

FORM
$request->exists($key);

Determine if the request contains a given input item key.
Input item key usually is a form field.

$request->has($key)

Determine if the request contains a non-empty value for an input item.
This checks if form field exists and has non-empty value.

$request->all();

Get all of the input (form) and files for the request.

$request->input($key = null, $default = null);

Retrieve an input item from the request.
If you:

  • do not set any value, you get all input values
  • if you set $key, you get input value for that key
  • you may also set $default, to get some value, if our keyed value returns no value
$request->only($keys);

Get a subset containing the provided keys with values from the input data.

You can pass single key (form field name):

You can pass array of keys (form field name):

$request->except($keys);

Same as one above, just you exclude selected keys.
You can also pass single key, or an array of keys, you want to exclude from your collection.

$request->intersect($keys);

Intersect an array of items with the input data.

intersect

So, you will get a collection with keys, that are present in array you passed, and in input.
You can pass single key, or an array.

$request->query($key = null, $default = null);

If you have a sql query in your Request object, you can retrieve it.

query

$request->old($key = null, $default = null);

This one works closely with flush().
It allows to get some session stored data, e.g. for form re-population etc.

session flash
  • flash($filter = null, $keys = [])
    Flash the input for the current request to the session.
  • flashOnly($keys)
    Flash only selected here values of the input to the session.
    You can pass array
  • flashExcept($keys)
    Flash only input values that are not listed here to the session.
$request->flush()

If you want to get rid of all stored old data from session, use this one.

$request->merge(array $input)

Merge new input into the current request’s input array.

$request->replace(array $input)

Replace the input for the current request.

$request->toArray()

Get all of the input and files for the request.
Alias of $request->all().

$request->__isset($key)

Check if an input element is set on the request.

$request->__get($key)

Get an input element from the request  by the key.

 

JSON
$request->json($key = null, $default = null)

Get the JSON payload for the request.
$key, if set specifies what to return.
You can set default value as well.

$request->isJson()

Determine if the request is sending JSON.
Returns bool.

$request->expectsJson()

Determine if the current request probably expects a JSON response.
Returns bool.

$request->wantsJson()

Determine if the current request is asking for JSON in return.
Returns bool.

$request->acceptsJson()

Determines whether a request accepts JSON.
Returns bool.

 

URL / PATH
$request->root();

Gets the root URL for the application, e.g.:

$request->url();

Gets entire URL with params, e.g:

… but no query string, like:

$request->fullUrl();

Gets entire URL with params and query string, e.g:

$request->fullUrlWithQuery();

Ads query string from array to url.

$request->path();

Returns path, e.g. to be used in route:

$request->decodedPath();

Decodes encoded path (via rawurlencode()).

e.g., we have this path:

By using above method, we get as result this:

$request->segment($index, $default = null);

This allows to pluck desired element of path – index starts at 1!

Lets say we have this url:

… and we want to pluck ‘profile’:

If we want to pluck element no. 4, which does not exist, we will get ‘null’, unless we set default:

Now, we get ‘default_stuff’ as a result.

$request->segment();

This will return all segments as an array.

Lets take url:

Result would be an array of all segments:

$request->is($pattern);

This returns true/false.

Lets say we have this url:

Now we match it against some pattern, e.g.:

Above will return true, since profile is there.
(*) asterisks denote wildcard characters.

Above works for segments (user/profile/stuff) only, not full url.

$request->fullUrlIs($pattern);

This works with full url – mot patterns only.

Consider url:

If I check against ‘la5’:

It will return ‘true’.

$request->route($param = null)

Get the route handling the request.

$request->method();

Returns HTTP verb used in request, e.g. GET, or PUT, or POST.

 

Other
$request->hasCookie($key);

Determine if a cookie is set on the request.

$request->cookie($key = null, $default = null);

Retrieve a cookie from the request.

$request->session()

Get the session associated with the request.

$request->secure();

This method checks, if request is over HTTPS.
Returns true/false.

$request->ip();

Returns the client IP address.

$request->ips();

Returns the client IP addresses.

And the result:

$request->server($key = null, $default = null);

Gets $_SERVER from request.

$request->accepts($contentTypes)

Determines whether the current requests accepts a given content type.
Returns bool.
Takes string, or array.
$contentTypes consists of MIME type – see list here.

$request->prefers($contentTypes)

Return the most suitable content type from the given array based on content negotiation.
Returns content type name.
Takes string, or array.
$contentTypes consists of MIME types – see list here.

$request->user($guard = null)

Get the user making the request.

$request->fingerprint()

A really great way to get an unique id for request / route / IP address.

Return may look like this: ef2dfffbdd7a2a9fbca6c5250ccbbe6caa4c09f0

$request->ajax();

This method checks, if request arrived by means of AJAX.
Returns true/false.

$request->pjax();

This method checks, if request arrived by means of PJAX.
Returns true/false.