表单验证
快速生成
php artisan make:requests LoginRequest
public function authorize() { return true; } /** * 表单验证的验证规则 * * @return array */ public function rules() { return [ 'BrandName' => 'filled|max:5', 'ProductCname' => 'filled|max:5|unique:product', 'UID' => 'required|Integer' ]; }
controller调用 需要引入 use AppHttpRequestsLoginRequest;
public function login(LoginRequest $request) { return $this->repo->check($request); }
项目逻辑
新建 Repositories文件夹,新建 LoginRepositories.php文件
<?php namespace AppRepositories; use Auth; class LoginRepositories { public function check($request) { $data = $request->only(['username', 'password']); $result = Auth::guard('admin')->attempt($data, $request->get('online')); if ($result) { return redirect(route('admin.index')); } else { return redirect(route('admin.login'))->withErrors(['loginerror' => '用户名密码错误']); } } }
controller调用 引入 use AppRepositoriesLoginRepositories;
protected $repo; public function __construct(LoginRepositories $repo) { $this->repo = $repo; }
public function login(LoginRequest $request) { return $this->repo->check($request); }