Laravel Requests: Accessing The Request

Accessing The Request

<?php 
namespace App\Http\Controllers;
use Illuminate\Http\Request; 
class UserController extends Controller
{ 
     /**
     * Store a new user.
     *
     * @param  Request  $request
     * @return Response
     */ public function store(Request $request)
     { 
          $name = $request->input('name');
          //
       }
 }

To obtain an instance of the current HTTP request via dependency injection, you should type-hint the Illuminate\Http\Request class on your controller method.

Dependency Injection & Route Parameters

Route::put('user/{id}', 'UserController@update');

You should list your route parameters after your other dependencies.

Accessing The Request Via Route Closures

Use Illuminate\Http\Request;

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

You may also type-hint the Illuminate\Http\Request class on a route Closure.

Request Path & Method

Accessing The Request: Request Path & Method — расположение

  • He Illuminate\Http\Request instance provides a variety of methods for examining the HTTP request for your application and extends the Symfony\Component\HttpFoundation\Request class.

PSR-7 Requests

Use Psr\Http\Message\ServerRequestInterface;

Route::get('/', function (ServerRequestInterface $request) {
    //
});
Composer require symfony/psr-http-message-bridge
composer require zendframework/zend-diactoros

You may obtain a PSR-7 request by type-hinting the request interface on your route Closure or controller method.

Laravel uses the Symfony HTTP Message Bridge component to convert typical Laravel requests and responses into PSR-7 compatible implementations.

Laravel Requests: Accessing The Request — Structure map

Clickable & Draggable!

Laravel Requests: Accessing The Request — Related pages: