Laravel 學習筆記之 request validation

在用laravel寫api時,當前端傳進來的request是POST/PUT/PATH等method時,那須要作request validation,儘管對於先後端分離程序,前端程序Angular/Vue已經作了validation,可是ajax傳過來的json input,在後端也須要作validation。前端

那該如何優雅的編寫request validation呢?laravel官方文檔已經包含了這個feature: Form Request Validationlaravel

這裏能夠寫一個JsonRequest:ajax

class JsonRequest extends Illuminate\Foundation\Http\FormRequest
{
    public function rules()
    {
        $method = $this->method();
        
        assert(in_array($method, [static::METHOD_POST, static::METHOD_PUT, static::METHOD_PATCH], true));
        
        $controller = $this->route()->getController();
        $rules      = $controller::RULES;

        return ($rules[$this->method()] ?? []) + ($rules['*'] ?? []);
    }

    public function authorize()
    {
        return true;
    }
}

這樣就能夠在衆多Model Controller裏使用JsonRequest就行,如:json

use Illuminate\Http\Request;

final class AccountController extends \App\Http\Controllers\Controller
{
    public const RULES = [
        Request::METHOD_POST => [
            'bank_account' => 'required_if:type,bank',
            'loan_account' => 'required_if:type,loan',
        ],
        Request::METHOD_PUT => [
            // ...
        ],
        '*' => [
            // ...
        ],
    ];
}

這樣就能夠校驗前端傳進來的json input是否合法。
(1)若是前端傳進來的json input是:後端

{
    "name": "lx1036",
    "type": "loan",
    "bank_account": {
        "source": "bank",
    }
}

那就validation失敗,不合法。
(2) 若是前端傳進來的json input是:api

{
    "name": "lx1036",
    "type": "bank",
    "loan_account": {
        "source": "loan",
    }
}

那就validation失敗,不合法。app

這樣就能夠校驗json input,不合法就直接彈回throw 一個HttpException,再也不用在進入下一步邏輯。對於這樣嵌套的json input,使用request validation來校驗對象間關係很重要,能夠看作是進入核心業務邏輯前的初步校驗。。固然最後寫表時還有model validation,避免壞數據進入db。前後端分離

最後一點,laravel文檔只是說了用法,沒有說明原理。代碼在\Illuminate\Foundation\Providers\FormRequestServiceProvider::class:ide

public function boot()
    {
        // \Illuminate\Foundation\Http\FormRequest use 了 ValidatesWhenResolvedTrait,extends 了 \Illuminate\Contracts\Validation\ValidatesWhenResolved
        $this->app->afterResolving(ValidatesWhenResolved::class, function ($resolved) {
            $resolved->validate();
        });

        // ...
    }

因此當從容器中resolve完\Illuminate\Foundation\Http\FormRequest後就會當即執行\Illuminate\Foundation\Http\FormRequest::validate()方法,具體不詳述,可看laravel源碼。ui

OK,總之,在寫程序時,validation很重要,須要去寫,包括request validation和model validation。。。

相關文章
相關標籤/搜索