[Lumen5.2 documentation] more features-verification

Source: Internet
Author: User
[Lumen5.2 documentation] more features-verification 1. Introduction

Lumen provides multiple methods to verify application input data. By default, the Lumen controller base class uses ValidatesRequeststrait, which provides a convenient way to verify input HTTP requests through various powerful verification rules.

2. Differences from Laravel

In most cases, the verification implementation in Lumen is the same as that in Laravel. you can learn more about the implementation details through the complete Laravel verification documentation, but there are still some differences between the two:

Verify in request

Lumen does not support input verification from requests. if you want to implement this function, you can only use the complete Laravel framework.

$ This-> validate method

Calling the $ this-> validate method in the Lumen controller always returns a JSON response with an error message. if it is not an AJAX request in Laravel, the method returns a response directly, the error message is stored in the Session. Because Lumen is stateless (The Session is not supported), the one-time Session error message is impossible. if you want to implement this function, you can only use the Laravel framework.

Unlike Laravel, Lumen provides the ability to access the validate method from the routing closure:

use Illuminate\Http\Request;$app->post('/user', function (Request $request) {    $this->validate($request, [        'name' => 'required',        'email' => 'required|email|unique:users'    ]);    // Store User...});

Of course, you can also use Validator: make to manually create a authenticator instance as in Laravel.

$ Errors view variables

Because Lumen no longer supports Session, the view variable $ errors is no longer valid. As mentioned earlier, if $ this-> validate fails to be verified, a JSON response with an error message is always returned, if you are not building a stateless API that only returns a JSON response, use the complete Laravel framework.

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.