How some

1. Receive requests

1.1 Basic use of Request

To pass the
Dependency injectionGets the current HTTP request instance, which you should import on the controller
Illuminate\Http\RequestClass. The incoming request instance will be made by
Service containerAutomatic injection.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class UserController extends Controller
{
    public function store(Request $request)
    {
        $name = $request->input('name');
        //
    }
}

1.2 Common methods of Request

The routing part

// Part One Route:: GET ('test/request', 'App/HTTP/Controllers/TestController@testGet'); // Part Two Route:: POST ('test/request', 'App/HTTP/Controllers/TestController@testPost');

Controller section

<? php use Illuminate\Http\Request; Class TestController extends Controller {/ / Part One: according to the url (GET | http://127.0.0.1:8000/test/request? Name = Bert) public function testGet(Request $Request) {$Request ->path(); $request-> (); $request-> (); $request-> (); / / print: "http://127.0.0.1:8000/test/request" / / get the complete url (with a get request parameters) $request - > fullUrl (); / / print: "http://127.0.0.1:8000/test/request? $request->method(); $request->method(); $request->getPort(); $request->getPort(); / / print output: 8000} / / Part Two: aimed at the url (Post | http://127.0.0.1:8000/test/request? Company =Tencent&name=bert&finger=king) /** * form-data  * name=kinra * option[]=A * option[]=B * option[]=C * key=null */ public function testPost(Request $request) { // $request-> dump($request-> dump()); $request-> = $request-> = $request-> = $request-> = $request-> = $request-> = $request-> = $request-> = $request->; / / print: "http://127.0.0.1:8000/test/request" / / get the complete url (parameters) dump ($request - > fullUrl ()); / / print: "http://127.0.0.1:8000/test/request? Company =anflk&finger=king&name= Bert "// dump($request->method()); $request-> dump($request->); $request-> dump($request->); $request->format($request->format($request->format($request->format())); // Print out: "HTML" // Get the request parameters. dump($request->query()); / / print: "the company" = > "Tencent", "name" = > "Bert", "finger" = > "king"] dump ($request - > input ()); // printout: ["name" => "kinra", "option" => [0 => "A", 1 => "B", 2 => "C"], "key" => null, "company" => "Tencent", "finger" => "king"] dump($request->all()); // printout: ["name" => "kinra", "option" => [0 => "A", 1 => "B", 2 => "C"], "key" => null, "company" => "Tencent", "Finger" => "king"] dump($request->input('company')); // printout: "Tencent" dump($request->company); $request->input('name'); $request->input('name'); // printout: "kinra" dump($request->query('name')); $request->input('option.0'); $request->input('option.0'); $request->input('option.0'); $request->has('name'); $request->has('name'); $request->has('name'); $request->has('name'); $request->has('namd'); $request->has(' name '); $request->has(['name', 'company']); $request->has(['name', 'company']); / / print: true dump ($request - > has ([' name 'and' email '])); $request->hasAny(['name', 'email']); $request->hasAny(['name', 'email']); / / print: true dump ($request - > hasAny ([' PWD 'and' email '])); $request->filled('name'); $request->filled('name'); $request->filled('name'); // printout: true dump($request->filled('key')); // printout: false}}

Note:

  1. Can be used when working with forms that contain arrays.Operator to access the array’s data
  2. The input method retrieves data from the entire request body (including the query string), while the Query method retrieves only input values from the query string. You can see the difference between the two methods above.

    dump($request->input('name')); // printout: "kinra" dump($request->query('name')); // Print out: "Bert"
  3. The value of a GET request parameter with the same name as the POST request parameter is overwritten by the same name as the POST request parameter.

1.3 Obtaining old data

Laravel allows you to keep data between requests. This feature is useful when you repopulate a form after a validation error.

  1. Premises for using old data: If the input of the last (pre) request is flash saved into the session, then the (this) request that follows the last request can obtain the input of the last (pre) request through the old() function, and the next (next) request cannot obtain the input of the last (pre) request. However, if this request has input and is flash stored, then the next request can get the input of this request.

    // Put the request input flash into the session for the next request. $request->flash(); Session $request->flashOnly(['name', 'email']); $request-> except (['name', 'email']); $request-> except (['name', 'email']); // Flash-save all input data to the session, except name and email
  2. $request->flash() can be redirected using a chain-like call withInput() instead of $request->flash();

    // A function in the controller. public function testSession(Request $request) { // $request->flash(); return redirect('test/request')->withInput(); } public function testSession($Request ->flash()) {$Request ->flash(); return redirect('test/request'); }
  1. Gets input data from the last flash saved to the session

    $request->old(); $request->old('username'); $request->old('username'); // Returns the value of the username entered in the last request