[Lumen5.2 documentation] basics-HTTP request 1. access request instance
To obtain the current HTTP Request instance through dependency injection, type prompts should be made to the Illuminate \ Http \ Request class in the controller's constructor or method. the current Request instance will be automatically injected by the service container:
Input ('name ');//}}
If your controller method also expects to obtain the route parameter input, you only need to place the route parameter after other dependencies. for example, if your route is defined as follows:
$app->put('user/{id}','UserController@update');
You can still type the Illuminate \ Http \ Request and define the controller method to access the routing parameters as follows:
1.1 Basic request information
The Illuminate \ Http \ Request instance provides multiple methods to detect HTTP requests of an application. The Illuminate \ Http \ Request of Lumen inherits from the Symfony \ Component \ HttpFoundation \ Request class, some useful methods in this class are listed here:
Retrieve request URI
The path method returns the requested URI. Therefore, if the requested path is http://domain.com/foo/bar, the path method returns foo/bar:
$uri=$request->path();
The is method allows you to verify whether the incoming request matches the given pattern. You can use the * wildcard when using this method:
if($request->is('admin/*')){ //}
To obtain the complete URL, not just the path information, you can call the url or fullUrl method of the request instance:
// Without the request parameter $ url = $ request-> url (); // with the request parameter $ url = $ request-> fullUrl ();
Request method
The method returns the HTTP request method of the request. You can also use the isMethod method to verify whether the HTTP request method matches the given string:
$method=$request->method();if($request->isMethod('post')){ //}
1.2 PSR-7 requests
The PSR-7 standard specifies an HTTP message interface, including requests and responses. If you want to get an PSR-7 request instance, first you need to install some libraries, Lumen uses the Symfony HTTP Message Bridge component to convert typical Lumen requests and responses to PSR-7-compatible implementations:
composer require symfony/psr-http-message-bridgecomposer require zendframework/zend-diactoros
After these libraries are installed, you only need to get the PSR-7 request in the routing or controller by type prompting the request type:
use Psr\Http\Message\ServerRequestInterface;$app->get('/', function (ServerRequestInterface $request) { //});
If a PSR-7 response instance is returned from a route or controller, it is automatically converted to a Lumen response instance and displayed.
2. get input Get input value
In some simple ways, you can access user input from the Illuminate \ Http \ Request instance. You do not need to worry about the HTTP request method used by the request, because the input access interfaces for all request methods are consistent:
$name = $request->input('name');
You can also pass a default value as the second parameter to the input method. if the input value of the request is not displayed in the current request, the value will be returned:
$name = $request->input('name', 'Sally');
When processing form array input, you can use "." to access the array:
$input = $request->input('products.0.name');$names = $request->input('products.*.name');
Determines whether the input value appears
You can use the has method to determine whether a value exists in the request. if the value exists and is not null, the has Method returns true:
if ($request->has('name')) { //}
Obtain all input data
You can also use the all method to obtain all input data:
$input = $request->all();
Obtain some input data
If you need to retrieve the subset of the input data, you can use the only or least t method. both methods receive an array or dynamic parameter list as a unique parameter:
$input = $request->only('username', 'password');$input = $request->only('username', 'password');$input = $request->except('credit_card');$input = $request->except('credit_card');
File Upload
Obtain uploaded files
You can use the file method of the Illuminate \ Http \ Request instance to access the uploaded files. the object returned by this method is an instance of the Symfony \ Component \ HttpFoundation \ File \ UploadedFile class, this class inherits from the SplFileInfo class that provides methods for file interaction in the PHP Standard Library:
$file = $request->file('photo');
Verify whether the file exists
Use the hasFile method to determine whether a file exists in the request:
if ($request->hasFile('photo')) { //}
Verify that the file has been uploaded
Use the isValid method to determine whether an error occurs during File upload:
if ($request->file('photo')->isValid()){ //}
Save uploaded files
Use the move method to save the uploaded file to a new path. this method moves the uploaded file from the temporary directory (configured in the PHP configuration file) to the specified new directory:
$request->file('photo')->move($destinationPath);$request->file('photo')->move($destinationPath, $fileName);
Other file methods
There are many other methods in the UploadedFile instance. view the APIs of this class to learn more.