Laravel Requests

Published January 29, 2022

You may occasionally need to interact with the current HTTP request handled by your Laravel 8 application or access the HTTP inputs, files, and cookies submitted with the request. With Illuminate/Http/Request, Laravel applications are able to communicate with current HTTP requests in an object-oriented manner.

In this chapter, we will examine the Laravel 8 requests. We will discuss:

  • How to access the Request

  • How to Request Method or Route/path

  • Request Headers

  • Request IP Address

  • The PSR-7 Requests

 

1. How to Access the Request

Through dependency injection, you can see the current HTTP request instance. You can gain access by type-hinting the Illuminate\Http\Request class on your controller method or route closure. As a result, the Laravel service container will inject the incoming request instance automatically.

Type-Hint on controller method

 

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class UserController extends Controller

{

    /**

     * Store, a new user.

     *

     * @param  \Illuminate\Http\Request  $request

     * @return \Illuminate\Http\Response

     */

    public function store(Request $request)

    {

        $name = $request->input('name');

 

        //

    }

}

 

Type-Hint on Route Closure

We can also type-hint the Illuminate/Http/Request class on the route closure. This enables the service container to inject the request automatically after the closure is executed

 

use Illuminate\Http\Request;

Route::get('/', function (Request $request) {

    //

});

 

2. How to Request Method or Route/path

Additionally, the Illuminate/Http/Request class offers a number of ways to inspect the incoming HTTP requests and extend the symfony\component\HttpFoundation\Request class. Let's examine how to retrieve the request path and inspect the request route.

 

a) How to Retrieve  the Request Route/path

The path method allows us to access the request path information. For example, if an incoming request targets http://samplesite.com/foo/bar, then the method will return foo/bar

 

$uri = $request->path();

 

b) How to Examine the Request Route/path

When inspecting the request path, we usually use the is method to check whether the incoming request matches the given pattern. When using this method,  you can also use the  * character as a wildcard

if ($request->is('admin/*')) {

    //

}

If you opt to use the routeIS method, you will know whether the incoming request has matched the named route

 

if ($request->routeIs('admin.*')) {

    //

}

 

c) Retrieve  the Request URL

If you would like to obtain the full URL of the incoming request, you only have to use the fullurl or url method.  However, the two are different. The urlmethod will return the URL without the query string, and on the other hand, the fullurl includes all the query strings.

 

d) How to retrieve  the Request method

If you want to know the request method used, you should use the method or the isMethod to verify whether it matches the provided string.

 

3. Request Headers

The Iluminate\Http\Request class can also be used to obtain the class headers.  If this method is used and there is no header, then the  null  will be returned

 

$value = $request->header('X-Header-Name');

$value = $request->header('X-Header-Name', 'default');

 

The hasHeader method is used to verify whether the request contains the  given header:

if ($request->hasHeader('X-Header-Name')) {

    //

}

 

4. Request IP Address

If you would like to retrieve the IP address of the client who made the request to your Laravel application, then you can do so by using the ip method

$ipAddress = $request->ip();

 

 

5. The PSR-7 Requests

PSR-7 is typically used to define the interfaces for HTTP messages, including both the responses and requests. If you would like to access the PSR-7 request instead of just the Laravel request, then you'll need to install a number of libraries, including the Symfonu HTTP Message Bridge component to convert laravel responses and requests into PSR-7 compatible implementations

 

composer require symfony/psr-http-message-bridge

composer require Nyholm/psr7

 

After running these commands to install these libraries, you can now obtain the PSR-7 request through type-hitting your request information in your controller method or the route closure. As shown below:

 

use Psr\Http\Message\ServerRequestInterface;

Route::get('/', function (ServerRequestInterface $request) {

    //

});

 

Conclusion

That’s it about the Laravel Requests

Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

207 Views