Author: Shiannizhilu public number: Zhixing Research Institute

14.1 the Request Request

1, the use method is very simple, in the Controller Controller, introduce the Request object, and in the Controller method, inject the Request object.

For example, run the following command to create the control class UserController:

php artisan make:controller UserController
Copy the code

At this point, the system automatically adds the following reference to the control class:

use Illuminate\Http\Request;
Copy the code

Then write the route, write the method, and you’re ready to test.

Route::get('user/index'.'UserController@index');
Copy the code

(input()); (input());

class UserController extends Controller
{
    // The method automatically gets the Request object, which is called injection
    public function index(Request $request){
        // The input method is used to retrieve the HTTP request data
        return $request->input('name'); }}Copy the code

At this time, access the link: http://127.0.0.1:8000/user/index? Name =ZhangSan, returns the string ZhangSan.

Of course, can increase more parameters, such as: http://127.0.0.1:8000/user/index? name=ZhangSan&id=10001

return $request->input('id').$request->input('name');
Copy the code

The result is 10001ZhangSan

The default value can be set for the request, that is, if the request parameters do not exist, the default value is used:

// The default value is 2
$request->input('name'.'no name');
Copy the code

There is also a dynamic way to obtain:

// Obtain it dynamically
$request->name;
Copy the code

3, use the all() method to get all the parameters passed by the URL;

return $request->all();
return $request->input();// This will do the same thing
Copy the code

Such as visit: http://127.0.0.1:8000/user/index? name=ZhangSan&id=10001&age=1024

Return JSON result:

{"name":"ZhangSan"."id":"10001"."age":"1024"}
Copy the code

4. If the route has parameters, the second and third parameters can be used.

For example, the route is changed to:

Route::get('user/{id}/{code}'.'UserController@index2');
Copy the code

Then the method of the controller is modified as:

public function index2(Request $request.$id.$code)
{
   / / content
}
Copy the code

Note here:

The sequence of route parameters must be the same as that of the controller

5, routing area, also support closure injection Request;

Route::get('/user'.function (Request $request) {
    return $request->all();
});
Copy the code

Use path() to get the current URI path.

Note: the following article, using the links: http://127.0.0.1:8000/user/index? Name =ZhangSan&id=10001&age=1024

return $request->path();
Copy the code

Results: the user/index

7. Use the is() method to determine whether the current URI matches.

// Is the URI of user/*, returns a Boolean value
return $request->is('user/*');
Copy the code

Results: 1.

8, use url() and fullUrl() to get url address, with and without parameter difference;

return $request->url();
return $request->fullUrl();
Copy the code

The results are as follows:

http://127.0.0.1:8000/user/index
http://127.0.0.1:8000/user/index?age=1024&id=10001&name=ZhangSan
Copy the code

Use isMethod() to determine the HTTP request mode, such as get, post, etc.

return $request->isMethod('post');
return $request->isMethod('GET');
Copy the code

The results are as follows:

(if false, no result is displayed) 1Copy the code

14.2 Other Common Request methods

1. How arrays are accepted

The front end form is as follows:

<form action="/user" method="get"> <input type="checkbox" name="select[][a]" value="1"> <input type="checkbox" Name ="select[][b]" value="2"> <input type=" select[][c]" value="3"> </button> </form>Copy the code

At this point, in the control class method, the way to get the request data:

return $request->input('select.1.b');// The output is 2
Copy the code

Do the same for JSON data;

2. Other utility methods for the Request object:

// Return a Boolean value
$request->boolean('name');

/ / returns the IP
$request->ip();

// Only fixed parameters are accepted
$request->only(['age'.'gender']);

// Exclude unnecessary arguments
$request->except(['name']);

// Check whether the parameter exists
return $request->has('name');

// Check whether all parameters exist
return $request->has(['name'.'age']);

// Return true if only one of the arguments exists
return $request->hasAny(['name'.'age']);

// Verify that the parameter exists and is not null
return $request->filled('name');

// Check that the parameter does not exist.
return $request->missing('name');
Copy the code

3. In addition to introducing Request in a method, you can also use the helper function Request () :

return request()->input();
Copy the code

14.3 Dependency Injection

Known in DataController, there are methods:

public function read($id)
{
    return ['id'= >$id];
}
Copy the code

If we wanted to use this method in our UserController, the original approach would be as follows:

$data = new DataController();
return $data->read(10);
Copy the code

The execution result is:

{"id":10}
Copy the code

To use dependency injection directly, you can use:

public function index(Request $request, DataController $data)
{
    return $data->read(10);
}
Copy the code

The above.