Laravel Auth - 验证失败后,将用户从功能验证器()重定向到不同的URL

时间:2017-03-20 14:26:28

标签: php laravel validation laravel-5.3 laravel-authorization

我尝试在mobile_no

的唯一验证失败后将用户重定向到其他页面
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
use App\User;
use App\Http\Controllers\Controller;

class RegisterController extends Controller
    {
        use RegistersUsers;

        protected function validator(array $data){
            $validator = Validator::make($data, $rules, $messages);
            // If unique validation fails
            if($validator->fails()){ 
                $failed = $validator->failed();
                // if failed because unique mobile number validation rule
                if(count($failed) == 1){
                    foreach ($failed as $key => $value) {
                        if($key == 'mobile_no'){
                            if(count($value) == 1 && isset($value['Unique'])){
                                return redirect('/verify');
                            }
                        }
                    }
                }
            return redirect('/somepage');
            }
        return $validator;
    }
}

当我尝试这样做时,它会给出错误

BadMethodCallException in RedirectResponse.php line 218:
Method [validate] does not exist on Redirect.

如何重定向到其他页面?我错过了什么。

1 个答案:

答案 0 :(得分:1)

validator方法应返回Validator。您看到的错误是因为您返回了Redirect响应,而某些代码假定您返回Validator并在其上调用$validator->validate()

实际通话可在RegistersUsers trait

中找到

您需要抛出HttpResponseException。您的异常处理程序应捕获这些并相应地呈现响应;在这种情况下执行重定向。