Laravel错误消息返回不起作用

时间:2016-10-05 11:49:32

标签: php laravel

Laravel 5.3 我无法找到解决方法如何回应错误消息我尝试所有方法flash->或会议没有运气!..

中间件 - > CheckStatus.php

 public function handle($request, Closure $next)
    {
        $response = $next($request);
        //If the status is not approved redirect to login
        if(Auth::check() && Auth::user()->roles != '2'){
            Auth::logout();

            return redirect('/login')->withErrors('email', 'Your error text');
        }
        return $response;
    }

路线

Route::post('/login', [
    'uses' => 'Auth\AuthController@login',
    'middleware' => 'checkstatus',
]);

Login.blade

@if ($errors->has('email'))
<span class="help-block">
   <strong>{{ $errors->first('email') }}</strong>
</span>
@endif
@if (count($errors))
<ul>
    @foreach($errors->all() as $error)
    <li>{{ $error }}</li>
    @endforeach
</ul>
@endif

我试试

return redirect('/login')->withErrors('email', 'Your error text');
return redirect('/login')->withErrors(['email', 'Your error text']);
return redirect('/login')->with('email', 'Your error text');

我试试

Route::group(['middleware' => ['web']], function () {});

My Kernel.php

protected $middleware = [
        \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
        \App\Http\Middleware\CheckStatus::class,

    ];

    /**
     * The application's route middleware groups.
     *
     * @var array
     */
    protected $middlewareGroups = [
        'web' => [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\Session\Middleware\StartSession::class,
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            \App\Http\Middleware\VerifyCsrfToken::class,
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
            \App\Http\Middleware\SetSiteLanguage::class,
        ],

        'api' => [
            'throttle:60,1',
            'bindings',
        ],
    ];

我无法收到错误消息....任何帮助。 感谢您的时间!

1 个答案:

答案 0 :(得分:0)

不要在$middleware数组中添加中间件。因为$middleware内的所有内容都会先加载。因此,当您的CheckStatus中间件加载时,Laravel Session尚未加载。

Kernel.php

所以请在$routeMiddleware数组中添加中间件,如下所示:

protected $routeMiddleware = [
    ...

    'CheckStatus' => \App\Http\Middleware\CheckStatus::class,
]

然后在CheckStatus中间件路由组中包装需要进行状态检查的所有路由。

实施例

Route::group(['middleware' => ['CheckStatus']], function () {
    ...
});

然后像这样更改中间件:

return redirect('/login')->withErrors(['email' => 'Your error text']);

请试试这个,希望它有所帮助。