欧美亚洲中文,在线国自产视频,欧洲一区在线观看视频,亚洲综合中文字幕在线观看

      1. <dfn id="rfwes"></dfn>
          <object id="rfwes"></object>
        1. 站長資訊網(wǎng)
          最全最豐富的資訊網(wǎng)站

          在FormRequest表單驗證器中獲取url中的值

          下面由Laravel教程欄目給大家介紹在FormRequest表單驗證器中獲取url中的值,希望對需要的朋友有所幫助!

          在FormRequest表單驗證器中獲取url中的值

          最近在自己做一個blog,根據(jù) Laravel項目開發(fā)規(guī)范來寫”優(yōu)雅”的代碼。
          項目的路由大概都是這樣的

          Route::get('/keywords','KeywordsController@index'); Route::get('/keywords/create','KeywordsController@create'); Route::post('/keywords/store','KeywordsController@store'); Route::delete('/keywords/{id}','KeywordsController@destory'); Route::get('/keywords/{id}/edit','KeywordsController@edit'); Route::put('/keywords/{id}','KeywordsController@update');

          驗證器用的是繼承FormRequest基類來驗證的,代碼如下

          <?php  namespace AppHttpRequests;use IlluminateValidationRule;class KeywordRequest extends Request{     public function rules()     {         //$this->route('id') 獲取url占位符為id的數(shù)據(jù)         switch ($this->method())         {             case  'POST' :             {                 return [                   'keyword' => 'required|unique:keywords'                 ];             }             case 'PUT':             case 'PATCH':             {               return [                 'keyword' => [                   'required',                   Rule::unique('keywords')->ignore($this->route('id')),                 ],               ];             }             case 'DELETE':             case 'GET':             default:             {                 return [];             }         }     }     public function messages()     {        return [            'keyword.required' => '關鍵字不能為空',            'id.required' => 'id不能為空',            'keyword.unique' => '關鍵字已存在,請重新填寫'        ];     }}

          根據(jù)請求的方法不同來進行驗證
          為了保持規(guī)范我在更新請求的時候的,并沒有把id放到form表單中,只放在了URL中,在官方文檔中也有這樣的方法。

          use IlluminateValidationRule; Validator::make($data, [     'email' => [         'required',         Rule::unique('users')->ignore($user->id),     ],]);

          但是這個$user->id一直不知道怎么獲取,在網(wǎng)上終于找到一個方法,符合我的要求

          $this->route('id')

          贊(0)
          分享到: 更多 (0)
          網(wǎng)站地圖   滬ICP備18035694號-2    滬公網(wǎng)安備31011702889846號