Laravel 5.2升级到5.3 flash消息错误

时间:2017-01-05 08:58:59

标签: laravel laravel-5.3

从Laravel 5.2升级到5.3后,我无法添加Flash消息。

这是我们使用的代码:

return redirect()->back()->with('alert-success', 'My Message');

用于显示消息:

@foreach (['danger', 'warning', 'success', 'info'] as $msg)
    @if(Session::has('alert-' . $msg))
        <div class="alert alert-{{ $msg }} alert-dismissible">
            <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
            {{ Session::get('alert-' . $msg) }}
        </div>
    @endif
@endforeach

如果我们提供错误的凭据,我们也无法使用登录消息:

<form class="form-horizontal" role="form" method="POST" action="{{ url('/login') }}">
    {{ csrf_field() }}
    <div class="form-group has-feedback {{ $errors->has('email') ? ' has-error' : '' }}">
        <input type="email" name="email" class="form-control" placeholder="Email" value="{{ old('email') }}">
        <span class="glyphicon glyphicon-envelope form-control-feedback"></span>
        @if ($errors->has('email'))
            <span class="help-block">
                <strong>{{ $errors->first('email') }}</strong>
            </span>
        @endif
    </div>
    <div class="form-group has-feedback {{ $errors->has('password') ? ' has-error' : '' }}">
        <input type="password" name="password" class="form-control" placeholder="Password">
        <span class="glyphicon glyphicon-lock form-control-feedback"></span>
        @if ($errors->has('password'))
            <span class="help-block">
                <strong>{{ $errors->first('password') }}</strong>
            </span>
        @endif
    </div>
    <div class="row">
        <!-- /.col -->
        <div class="col-xs-12">
            <button type="submit" class="btn btn-primary btn-block btn-flat">
                Login
            </button>
        </div>
    </div>
</form>

2 个答案:

答案 0 :(得分:1)

如果您使用5.2.27或更低版​​本并移至5.3,那么您肯定在路由文件中使用了web中间件。您需要Example Fiddle,因为从5.2.27开始,如果您尝试将其手动添加到web.php,则会制定与会话相关的功能。

答案 1 :(得分:0)

解决方案可以在Kernel.php中进行编辑。

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

    \Illuminate\Session\Middleware\StartSession::class,
    \Illuminate\View\Middleware\ShareErrorsFromSession::class,
];

当我添加StartSessionShareErrorsFromSession时,它就像旧网站一样。