php - 验证错误后的 Laravel 自定义重定向

标签 php laravel laravel-validation

请问我在 LoginRequest.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');
    }
}

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

最佳答案

如果你想重定向到一个特定的 url,那么使用 protected $redirect

class LoginRequest extends Request
{
    protected $redirect = "/login#form1";

    // ...
}

或者如果你想重定向到命名路由,则使用 $redirectRoute

class LoginRequest extends Request
{
    protected $redirectRoute = "session.login";

    // ...
}

关于php - 验证错误后的 Laravel 自定义重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30544779/

相关文章:

PHP 生成器产生第一个值,然后遍历其余值

php - 在 PHP 中,如何以 JSON 格式从 MySQL 返回多个列值?

php - MYSQL 语法错误 : Where

mysql - 为什么 Laravel 5.7 中 DB::raw 变量从字符串修改为整数

laravel - required_without 不使用其他规则

php - Laravel 5,使用 withErrors($validator) 时 $errors 数组始终为空

java - 如何使用 $_GET 将 Textview 值放入 URL 中以从 PHP 接收

laravel - 更改 Laravel Blade 分隔符

mysql - 从 Laravel 5.1 升级到 Laravel 5.8 后,whereHas() 速度变慢

php - 尽管在 Laravel 5 中使用唯一验证,但字段重复