laravel实践12.flash提示信息

session()->flash()方法存入一条缓存的数据,让它只在下一次的请求内有效。常用于重定向后显示提示信息。

$user = User::create([
            'name' => $request->name,
            'email' => $request->email,
            'password' => bcrypt($request->password),
        ]);

        session()->flash('success', '您已注册成功!');
        return redirect()->route('users.show', [$user]);

设置一个公用视图,使其显示缓存信息。

resources/views/shared/_messages.blade.php

@foreach (['danger', 'warning', 'success', 'info'] as $msg)
  @if(session()->has($msg))
    <div class="flash-message">
      <p class="alert alert-{{ $msg }}">
        {{ session()->get($msg) }}
      </p>
    </div>
  @endif
@endforeach

将消息提醒公用视图嵌入主通用视图

resources/views/layouts/default.blade.php

<body>
    @include('layouts._header')
    <div class="container">
      <div class="offset-md-1 col-md-10">
        @include('shared._messages')
        @yield('content')
        @include('layouts._footer')
      </div>
    </div>
  </body>

 

posted @ 2019-12-27 17:25  GIC扫地僧  阅读(399)  评论(0编辑  收藏  举报