![]()
WARNING You're browsing the documentation for an old version of Laravel. Consider upgrading your project to Laravel 11.x.
The Request facade will grant you access to the current request that is bound in the container. For example:
$name = Request::input('name');
Remember, if you are in a namespace, you will have to import the Request facade using a use Request; statement at the top of your class file.
To obtain an instance of the current HTTP request via dependency injection, you should type-hint the class on your controller constructor or method. The current request instance will automatically be injected by the service container:
<?php namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
class UserController extends Controller {
/**
* Store a new user.
*
* @param Request $request
* @return Response
*/
public function store(Request $request)
{
$name = $request->input('name');
//
}
}
If your controller method is also expecting input from a route parameter, simply list your route arguments after your other dependencies:
<?php namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
class UserController extends Controller {
/**
* Update the specified user.
*
* @param Request $request
* @param int $id
* @return Response
*/
public function update(Request $request, $id)
{
//
}
}
Using a few simple methods, you may access all user input from your Illuminate\Http\Request instance. You do not need to worry about the HTTP verb used for the request, as input is accessed in the same way for all verbs.
$name = Request::input('name');
$name = Request::input('name', 'Sally');
if (Request::has('name'))
{
//
}
$input = Request::all();
$input = Request::only('username', 'password');
$input = Request::except('credit_card');
When working on forms with "array" inputs, you may use dot notation to access the arrays:
$input = Request::input('products.0.name');
Laravel also allows you to keep input from one request during the next request. For example, you may need to re-populate a form after checking it for validation errors.
The flash method will flash the current input to the session so that it is available during the user's next request to the application:
Request::flash();
Request::flashOnly('username', 'email');
Request::flashExcept('password');
Since you often will want to flash input in association with a redirect to the previous page, you may easily chain input flashing onto a redirect.
return redirect('form')->withInput();
return redirect('form')->withInput(Request::except('password'));
To retrieve flashed input from the previous request, use the old method on the Request instance.
$username = Request::old('username');
If you are displaying old input within a Blade template, it is more convenient to use the old helper:
{{ old('username') }}
All cookies created by the Laravel framework are encrypted and signed with an authentication code, meaning they will be considered invalid if they have been changed by the client.
$value = Request::cookie('name');
The cookie helper serves as a simple factory for generating new Symfony\Component\HttpFoundation\Cookie instances. The cookies may be attached to a Response instance using the withCookie method:
$response = new Illuminate\Http\Response('Hello World');
$response->withCookie(cookie('name', 'value', $minutes));
By "forever", we really mean five years.
$response->withCookie(cookie()->forever('name', 'value'));
You may also "queue" a cookie to be added to the outgoing response, even before that response has been created:
<?php namespace App\Http\Controllers;
use Cookie;
use Illuminate\Routing\Controller;
class UserController extends Controller
{
/**
* Update a resource
*
* @return Response
*/
public function update()
{
Cookie::queue('name', 'value');
return response('Hello World');
}
}
$file = Request::file('photo');
if (Request::hasFile('photo'))
{
//
}
The object returned by the file method is an instance of the Symfony\Component\HttpFoundation\File\UploadedFile class, which extends the PHP SplFileInfo class and provides a variety of methods for interacting with the file.
if (Request::file('photo')->isValid())
{
//
}
Request::file('photo')->move($destinationPath);
Request::file('photo')->move($destinationPath, $fileName);
There are a variety of other methods available on UploadedFile instances. Check out the API documentation for the class for more information regarding these methods.
The Request class provides many methods for examining the HTTP request for your application and extends the Symfony\Component\HttpFoundation\Request class. Here are some of the highlights.
$uri = Request::path();
if (Request::ajax())
{
//
}
$method = Request::method();
if (Request::isMethod('post'))
{
//
}
if (Request::is('admin/*'))
{
//
}
$url = Request::url();
last update:2017-05-16 00:44