這篇文章將為大家詳細講解有關(guān)在FormRequest表單驗證器中怎樣獲取url中的值,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

為梨樹等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計制作服務(wù),及梨樹網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務(wù)為成都網(wǎng)站設(shè)計、做網(wǎng)站、梨樹網(wǎng)站設(shè)計,以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達到每一位用戶的要求,就會得到認可,從而選擇與我們長期合作。這樣,我們也可以走得更遠!
最近在自己做一個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 App\Http\Requests;use Illuminate\Validation\Rule;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' => '關(guān)鍵字不能為空',
'id.required' => 'id不能為空',
'keyword.unique' => '關(guān)鍵字已存在,請重新填寫'
];
}}根據(jù)請求的方法不同來進行驗證
為了保持規(guī)范我在更新請求的時候的,并沒有把id放到form表單中,只放在了URL中,在官方文檔中也有這樣的方法。
use Illuminate\Validation\Rule;
Validator::make($data, [
'email' => [
'required',
Rule::unique('users')->ignore($user->id),
],]);但是這個$user->id一直不知道怎么獲取,在網(wǎng)上終于找到一個方法,符合我的要求
$this->route('id')關(guān)于“在FormRequest表單驗證器中怎樣獲取url中的值”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
當前名稱:在FormRequest表單驗證器中怎樣獲取url中的值
路徑分享:http://chinadenli.net/article26/ipjcjg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè)、域名注冊、靜態(tài)網(wǎng)站、小程序開發(fā)、網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)