Servage Magazine

Information about YOUR hosting company – where we give you a clear picture of what we think and do!

Working with Laravel request-data

Monday, April 20th, 2015 by Servage

request-programming-appIn previous articles the usage of the Laravel PHP framework was described up to the point of setting up the framework and getting a basic controller running via a HTTP route. This is the basis for getting anything working via a browser request to a Laravel powered website. However, requests can include much more information than the simple URL. Many kinds of data could be passed, depending on your use case. For example it could be cookie information used for user login handling or shopping cart storage, post form data for user submitted information, or get parameters to manipulate information display on the given page. All this information is available in your Laravel PHP code via the request class.

Working with request data is usually something you do in the controller according to the MVC pattern. However, nothing really prevents you from using the request data anywhere in your code where you see fit.

Use Laravel’s request class instead of native PHP variables

The purpose of Laravel’s built-in request handling is to standardize the interface through which data is collected. This facilitates a streamlined implementation of request and data handling, and enables Laravel to trust the data interface to a higher extent than the native PHP variables $_REQUEST, $_GET and $_POST. Therefore it is recommend not to use the native variables unless you specifically need to. Otherwise refer to the request class. Below you find various ways in which you can use the Laravel request class to access and check data. Learning these methods is pretty straightforward.

Request data via facade

The simplest way to get request data anywhere in the code is via the facade Laravel, which by default offers availability for the request class (note that you need to “use” the relevant class if you are working inside a namespace).

use Request;
$name = Request::input('name');

Request data via dependency injection

You can typehint the request class to be included in any given controller by including it in the controller’s constructor or method.

public function __constructor(Request $request)
    $name = $request->input('name');

public function yourMethod(Request $request)
    $name = $request->input('name');

public function yourMethodWithRouteParams(Request $request, $id, $name, $etc)
    $name = $request->input('name');

Note how you can still use expected values from the route in the method.

Getting an input value

$name = Request::input('name');
$name = Request::input('name', 'Sally'); // With default value

Checking if an input value is present

if (Request::has('name'))
    // Request has specified data ...

Getting a cookie value

$value = Request::cookie('name');

Getting a file

$file = Request::file('photo');

Checking if a file is present

if (Request::hasFile('photo'))
    // Request has specified file ...

Getting and checking request method

// Get the request method 
$method = Request::method();

// Check the request method
if (Request::isMethod('post'))
    // Request is post

Other uses for request data

There are many more methods available in the request class, so therefore taking a look at Laravel’s official API documentation is an advantage to figuring out more advanced uses before attempting to built something yourself. Whatever data you need to get or check, it is likely to be already possible with the built-in request class.

Working with Laravel request-data, 3.8 out of 5 based on 4 ratings
Categories: Guides & Tutorials

Keywords: ,

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

No comments yet (leave a comment)

You are welcome to initiate a conversation about this blog entry.

Leave a comment

You must be logged in to post a comment.