To obtain an instance of the current HTTP request, 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 {
/**
* Store a new 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->get('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 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'));
$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();
$method = $request->method();
if ($request->isMethod('post'))
{
//
}
if ($request->is('admin/*'))
{
//
}
$url = $request->url();
There are a variety of other methods available on Request
instances. Check out the API documentation for the class for more information regarding these methods.