简体   繁体   English

Laravel 5.4 验证失败后不会重定向

[英]Laravel 5.4 does not redirect after unsuccessful validation

I am using basic validation in Laravel (5.4) Controller.我在 Laravel (5.4) 控制器中使用基本验证。 If USER submits the form with empty field's the validation is unsuccessful but it won't redirect to the page where the request came from and rather gives me a white blank page.如果 USER 提交带有空字段的表单,则验证不成功,但它不会重定向到请求来自的页面,而是给我一个白色的空白页面。 If I click back button on the browser window then I can see the standard error messages on the screen that the fields are empty.如果我单击浏览器窗口上的后退按钮,则可以在屏幕上看到字段为空的标准错误消息。

I checked the laravel.log and it gives bunch of errors.我检查了laravel.log ,它给出了一堆错误。 I have added the headings of the errors below.我在下面添加了错误的标题。

And this issue is with every form on the project.这个问题存在于项目中的每个表单中。 Please help me to solve this issue, THANKS in advance.请帮我解决这个问题,提前致谢

Routes: web.php路线: web.php

Route::prefix($language)->middleware(['CheckReferer', 'CheckCart'])->group(function() {
    Route::get('/signin', 'Auth\UserLoginController@getSignIn')->name('user.signIn');
    Route::post('/signin', 'Auth\UserLoginController@postSignIn')->name('user.postSignIn');
}

sign-in.blade.php登录.blade.php

<form name="form_signup" id="form_signup" action="{{ route('user.postSignIn') }}" method="post">
    <div class="card">
        <div class="card-block">

            <!--Header-->
            <div class="form-header blue-gradient">
                <h3><i class="fa fa-user"></i> {{ __('sign-in.sign_in') }}</h3>
            </div>

            <!-- Form validation Errors -->
            @if ($errors->any())
                <div class="alert alert-danger">
                    <ul>
                        @foreach ($errors->all() as $error)
                            <li>{{ $error }}</li>
                        @endforeach
                    </ul>
                </div>
            @endif
            <!-- END Form validation Errors -->

            <!--Body-->
            <div class="md-form">
                <i class="fa fa-envelope text-blue-color prefix"></i>
                <input type="text" id="user_email" name="user_email" class="form-control" value="">
                <label for="user_email">{{ __('sign-in.your_email') }}</label>
            </div>

            <div class="md-form">
                <i class="fa fa-lock text-blue-color prefix"></i>
                <input type="password" id="user_password" name="user_password" class="form-control" value="">
                <label for="user_password">{{ __('sign-in.password') }}</label>
            </div>

            <div class="text-center">
                {{ csrf_field() }}

                <button type="submit" class="btn btn-primary">{{ __('sign-up.btn_login') }}</button>
            </div>

        </div>
    </div>
</form>

loginController.php登录控制器.php

public function postSignIn(CookieJar $cookieJar, Request $request) {

        $this->validate($request, [
            'user_email' => 'email|required',
            'user_password' => 'required|min:3'
        ]);

        if(Auth::guard('web')->attempt(['user_email' => $request->user_email, 'password' => $request->user_password])) {
            // Successful
            $cookieJar->queue(cookie('userID', Auth::id(), 1440));
            return redirect()->route('home');
        }

        return redirect()->back();

}

laravel.log laravel.log

local.ERROR: ErrorException: Trying to get property 'headers' of non-object in /Applications/MAMP/htdocs/project/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php:156
Stack trace:
.
.
.
local.ERROR: Symfony\Component\Debug\Exception\FatalThrowableError: Type error: Argument 1 passed to Illuminate\Session\Middleware\StartSession::addCookieToResponse() must be an instance of Symfony\Component\HttpFoundation\Response, null given, called in /Applications/MAMP/htdocs/project/vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php on line 72 in /Applications/MAMP/htdocs/project/vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php:170
Stack trace:
.
.
.
local.ERROR: Symfony\Component\Debug\Exception\FatalThrowableError: Type error: Argument 1 passed to Illuminate\Cookie\Middleware\EncryptCookies::encrypt() must be an instance of Symfony\Component\HttpFoundation\Response, null given, called in /Applications/MAMP/htdocs/project/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/EncryptCookies.php on line 59 in /Applications/MAMP/htdocs/project/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/EncryptCookies.php:123
Stack trace:
.
.
.
local.ERROR: ErrorException: Trying to get property 'headers' of non-object in /Applications/MAMP/htdocs/project/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php:156
Stack trace:
.
.
.
local.ERROR: Symfony\Component\Debug\Exception\FatalThrowableError: Type error: Argument 1 passed to Illuminate\Session\Middleware\StartSession::addCookieToResponse() must be an instance of Symfony\Component\HttpFoundation\Response, null given, called in /Applications/MAMP/htdocs/project/vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php on line 72 in /Applications/MAMP/htdocs/project/vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php:170
Stack trace:
.
.
.
local.ERROR: Symfony\Component\Debug\Exception\FatalThrowableError: Type error: Argument 1 passed to Illuminate\Cookie\Middleware\EncryptCookies::encrypt() must be an instance of Symfony\Component\HttpFoundation\Response, null given, called in /Applications/MAMP/htdocs/project/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/EncryptCookies.php on line 59 in /Applications/MAMP/htdocs/project/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/EncryptCookies.php:123
Stack trace:
.
.
.

Did you include csrf token on form?您是否在表单中包含 csrf 令牌?

<form method="POST" action="/profile">
    @csrf
    ...
</form>

You can also change this for the route of the original form您也可以为原始表单的路线更改此设置

//return redirect()->back();
return redirect('user.signIn');

I finally figured out a solution which worked out for me.我终于想出了一个对我有用的解决方案。 I wrapped the validation in the try and catch.我将验证包装在 try 和 catch 中。 Below is the code for my issue.以下是我的问题的代码。

try {
    $this->validate($request, [
        'user_email' => 'email|required',
        'user_password' => 'required|min:3'
    ]);
} catch (\Illuminate\Validation\ValidationException $e){
    return redirect()->back();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM