<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                如果是更復雜的驗證場景,你可能需要創建一個"表單請求"。表單請求是一個自定義的請求類包含了一些驗證的邏輯。你可以通過 `Artisan `的命令行 make:request 來創建一個表單請求類。 ~~~ php artisan make:request StoreBlogPostRequest ~~~ 生成的類會放置在 `app/Http/Requests` 目錄中。 我們在 `rules `方法中增加一些驗證規則: ~~~ /** * Get the validation rules that apply to the request. * * @return array */ public function rules() { return [ 'title' => 'required|unique|max:255', 'body' => 'required', ]; } ~~~ 那么,我們的驗證規則是怎么執行的呢?你所要做的只是在控制器方法中加上請求的類型提示: ~~~ /** * Store the incoming blog post. * * @param StoreBlogPostRequest $request * @return Response */ public function store(StoreBlogPostRequest $request) { // The incoming request is valid... } ~~~ 當控制器的方法被調用前,表單請求已經驗證了,意味著你不需要在控制器里寫任何的驗證邏輯。它已經驗證完了! 如果驗證失敗,用戶會收到一個重定向請求至上一個頁面。而錯誤信息也已經存儲至 session 中方便視圖展示。如果收到的是一個 AJAX 請求,一個帶有 422 狀態碼的 HTTP 響應會被返回給瀏覽器,包含了一個含有錯誤信息的 JSON 對象。 ## 授權表單請求 表單請求類同樣也包含了一個 authorize 方法。通過這個方法,你可以檢查認證后的用戶是否有權限去更新一個已有的資源。比如,如果一個用戶嘗試去更新一篇博客的評論,他是否真的發布過這個評論?舉個例子: ~~~ /** * Determine if the user is authorized to make this request. * * @return bool */ public function authorize() { $commentId = $this->route('comment'); return Comment::where('id', $commentId) ->where('user_id', Auth::id())->exists(); } ~~~ 注意上面例子中調用的 route 方法。這個方法允許你獲取調用路由中定義的 URI 參數,比如下面例子中的 {comment} 參數: ~~~ Route::post('comment/{comment}'); ~~~ 如果 `authorize `方法返回 false, 一個帶有 403 狀態碼的 HTTP 響應會被返回給瀏覽器,你控制器的方法也不會被執行。 如果你打算在應用的其他地方做一些權限的邏輯,在 authorize 方法中返回 true 即可: ~~~ /** * Determine if the user is authorized to make this request. * * @return bool */ public function authorize() { return true; } ~~~ ## 自定義閃存后的錯誤信息格式 如果你想要自定義驗證失敗后已經閃存至 session 的錯誤消息格式,可以通過覆蓋基類請求類(`App\Http\Requests\Request`)的 `formatErrors`。不要忘記在文件頂部引入 `Illuminate\Validation\Validator` 類: ~~~ /** * {@inheritdoc} */ protected function formatErrors(Validator $validator) { return $validator->errors()->all(); } ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看