[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.