HTTP request for Laravel #

Source: Internet
Author: User

GET Request #

To get an instance of the current Http request through dependency injection, you should type hints in the Controller method illuminate\http\request

An instance of an incoming request is automatically injected through the service container:

<?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 ');

}

}

Dependency Injection & Routing Parameters #

If the Controller method is going to fetch data from the routing parameters, then the routing parameters should be listed after the other dependencies. For example, if your route is defined like this:

Route::p ut (' User/{id} ', ' [email protected] ');

Using the type hint Illuminate\http\request as shown below, you can get the route parameter ID by defining the Controller method:

<?php

namespace App\http\controller;

Use Illuminate\http\request;

Class Usercontroller extends Controller

{

/*

Update the specified user

@param Request $request

@param string $id

@return Response

*/

Public Function Update (Request $request, $id)

{

//

}

}

Get requests by routing closures #

You can also prompt the Illuminate\http\request class in the routing closure, and the service container will automatically inject the current request into the closure when it executes:

Use Illuminate\http\request;

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

Request Path & Method #

The Illuminate\http\request instance provides several ways to check an application's Http request and inherit the

Symfony\component\httpfoundation\request class. Here are a few useful methods for this class:

Get Request Path #

The path method returns the route information for the request. That is, if the destination address of the incoming request is Http://domain.com/foo/bar, then path will

return to Foo/bar;

$uri = $request->path ();

The Is method can verify the incoming request path and specify whether the rule matches. With this method, you can also pass a * character as a wildcard character:

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

Get the requested url#

You can use the URL or the FullUrl method to get the full Url,url method of the incoming request to return a URL without a query string, and the return value of the FullUrl method

Contains the query string:

Without Query String ...

$url = $request->url ();

With Query String ...

$url = $request->fullurl ();

Get Request Method #

For the incoming request method, returns the HTTP request mode. You can also use the Ismethod method to verify that the HTTP request is in the specified

Whether the rules match:
$method = $request->mothod ();

if ($request->ismethod (' post ')) {}

Input Preprocessing & Normalization #

By default, Laravel contains the trimstrings and convertemptystringstonull two middleware in the application's global middleware stack.

These middleware are made up of app\http| The Kernel class is listed in the stack. They will automatically process all incoming string fields on the request, and will empty the string

The field is converted to a null value. This way you don't have to worry about data normalization in routing and controllers.

If you want to deactivate these features, you can remove both middleware from the application's middleware stack, just the App\http\kernel class's

Remove them $middleware properties.

Get Input #

Get all input data #

You can use the all method to get all the input data in an array form:

$intput = $request->all ();

Gets the specified input value #

Using a few simple methods (without specifically specifying that Http action), you can access all the user input in the Illuminate\http\request instance,

In other words, regardless of the HTTP action, the input method can be used to get the user input data:

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

You can pass a default value to the second parameter of the input method. If the requested input value does not exist on the request, the default value is returned:

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

If you are transferring data from the form data in the form of [array], you can use the [point] syntax to get the array:

$name = $request->input (' prodects.0.name ');

$names = $request->input (' products.*.name ');

Get input from query string #

Use the input method to get input data (including query strings) from the entire request, and the query method can only get input data from the query string

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

If the requested query string does not exist, the second parameter of the method is returned:

$name = $request->query (' name ', ' Helen ');

You can call the query method without supplying arguments to associate the rows of an array or to retrieve all query string values:

$query = $request->query ();

Get input through Dynamic properties #

You can also get user input by illuminate\http\request the dynamic properties of the instance. For example, if you apply a form that contains a name field,

Then you can access the value of the field in this way:

$name = $request->name;

The priority of Laravel in handling dynamic properties is to look in the requested data first, if not, and then to the route parameter summary.

Get JSON input Information #

If the request data sent to the application is JSON, the input method can be used as long as the requested Content-type header is correctly set to Application/json

By accessing the JSON data, you can even use the [point] syntax to read the JSON array:
$name = $request->input (' User.Name ');

Get partial input data #

If you need to get a subset of the input data, you can use the only and except methods, both of which receive arrays or dynamic lists as parameters

$input = $request->only ([' username ', ' password ']);

$input = $request->only (' username ', ' password ');

$input = $request->except ([' Credit_card ']);

$input = $request->except (' Credit_card ');

The only method returns all key-value pairs that you specify, but does not return a key-value pair that does not exist for the request rollup.

Determine if an input value exists #

To determine if a value exists for a request, you can use the has method. If the value is present in the request rollup, the has method returns true;

if ($request->has (' name '))

{

}

When an array is supplied as a parameter, the has method determines whether there are any given values in the data:

if ($request->has ([' Name ', ' email '])

{

//

}

You can use the filled method if you want to determine whether the request has a value and is not empty;

if ($request->filled (' name '))

{

//

}

HTTP request for Laravel #

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.