因为学习过了5.1之后,基本的东西就不再做介绍了
自定义错误返回机制一
php artisan make:request bannerCheckUpload;
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class bannerCheckUpload extends FormRequest
{
public function authorize()
{
return true;
}
public function rules()
{
return [
'alt' => 'bail|required|max:45'
];
}
public function messages()
{
return [
'alt.required' => '图片的提示不能为空'
];
}
}
<?php
namespace App\Http\Controllers;
use App\Http\Requests\bannerCheckUpload;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Contracts\Validation\Validator;
class BannerController extends Controller
{
public function uploading(bannerCheckUpload $request)
{
}
}
自定义错误返回机制二
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class BannerController extends Controller
{
public function uploading(Request $request)
{
$message = [
'alt.required'=>'账号不能为空!',
'link.required'=>'密码不能为空!',
];
$this->validate($request, [
'alt' => 'required|max:10',
'link' => 'required',
],$message);
}
}
网友评论