Learning notes for Lumen 5.2 HTTP requests

Source: Internet
Author: User
Tags file upload http request require

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:

<? Php

Namespace App \ Http \ Controllers;

Use Illuminate \ Http \ Request;
Use Illuminate \ Routing \ Controller;

Class UserController extends Controller
{
/**
* Store new users
     *
* @ Param Request $ request
* @ Return Response
*/
Public function store (Request $ request)
    {
$ Name = $ request-> 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:

<? Php
   
Namespace App \ Http \ Controllers;
   
Use Illuminate \ Http \ Request;
Use Illuminate \ Routing \ Controller;

ClassUser Controller extends Controller
{
/**
* Update a specified user
     *
* @ Param Request $ request
* @ Param int $ id
* @ Return Response
*/
Public function update (Request $ request, $ id)
    {
//
    }
}

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, pathpattern will return 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 request parameters
$ Url = $ request-> url ();

// With request parameters
$ 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-bridge
Composer 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-> response t ('credit _ card ');
$ Input = $ request-> response t ('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.

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.