我能问一下我在登录请求中做错了什么吗.我在php中设置了一个条件,如果登录过程中出现任何错误,可以重定向到自定义登录页面?我的代码如下:

<?php namespace App\Http\Requests;

use App\Http\Requests\Request;

class LoginRequest extends Request
{

    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
        'login_email'               =>  'required',
        'login_password'            =>  'required'
        ];
    }


    public function messages()
    {
        return [
            'login_email.required'          =>  'Email cannot be blank',
            'login_password.required'       =>  'Password cannot be blank'
        ];
    }

    public function redirect()
    {
        return redirect()->route('login');
    }
}

如果出现任何错误,该代码应该将从导航栏登录表单登录的用户重定向到主登录页面,但它似乎没有重定向.

推荐答案

找到了一个解决方案.我所需要做的就是覆盖

请求.php

就像这样,它就像一个符咒.

public function response(array $errors)
{
    // Optionally, send a custom response on authorize failure 
    // (default is to just redirect to initial page with errors)
    // 
    // Can return a response, a view, a redirect, or whatever else

    if ($this->ajax() || $this->wantsJson())
    {
        return new JsonResponse($errors, 422);
    }
    return $this->redirector->to('login')
         ->withInput($this->except($this->dontFlash))
         ->withErrors($errors, $this->errorBag);
}

Laravel相关问答推荐

Inertia React中的错误文件上传更新

如何通过单击进入 vs 代码中的类

Laravel:如何强制使用 HTTPS?

基于数据表 Laravel 4 导出 CSV 或 PDF

Laravel,没有shell 访问的转储自动加载

Laravel 不验证是否不需要字段

Laravel 5.3 auth 判断构造函数返回 false

Laravel 查询构建器 - 如何按别名分组,或进行原始 groupBy

laravel 中的 Http Post 使用 fetch api 给出 TokenMismatchException

Laravel 5 - Php artisan语法错误

如何在 Laravel 中创建类别的嵌套列表?

使用数据库中的值进行动态邮件配置 [Laravel]

Laravel Artisan CLI 安全地停止守护进程队列工作者

在 laravel 6.0 中未定义命令ui

Laravel 无法创建根目录

使用 Laravel 创建新项目会引发异常

Laravel Mail 发送Electron邮件但返回 false

Laravel php artisan 产生错误

如何在 Laravel 测试中禁用选定的中间件

如何在没有日志(log)、没有信息的情况下调试 Laravel 错误 500