会话闪存在laravel 5.2中不起作用

时间:2016-04-30 06:38:53

标签: php laravel-5.2

我是Laravel 5.2的新用户我正在尝试使用会话闪存来显示警报

以下是会话flash的代码

DepartmentContoller.php

public function add(Request $request ){

        if($request->isMethod('post'))
        {
            $department = new Department;

            /*$department->department_name = $request->department_name;
            $department->status = $request->status;
            $department->save();*/

            $department->create($request->all());
            $request->session()->flash('alert-success', 'User was successful added!');
            return redirect('admin/departments');
        }

        return view('admin.department.add');
    }

这是我要显示会话闪存值的视图文件

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

这是我的路线deatail

Route::group(['middleware' => ['web']], function () {
//Login Routes...
Route::get('/admin', 'AdminAuth\AuthController@showLoginForm');
Route::get('/admin/login','AdminAuth\AuthController@showLoginForm');
Route::post('/admin/login','AdminAuth\AuthController@login');
Route::get('/admin/logout','AdminAuth\AuthController@logout');

// Registration Routes...
Route::get('admin/register', 'AdminAuth\AuthController@showRegistrationForm');
Route::post('admin/register', 'AdminAuth\AuthController@register');    

Route::group(['as' => 'admin', 'middleware' => ['admin', 'web'], 'prefix'=>'admin'], function () {
    Route::get('/admin', array('uses'=>'AdminController@index'));
    Route::get('/departments', array('as' => 'departments', 'uses'=>'DepartmentController@index'));
    Route::get('/department/add', array('as' => 'add', 'uses'=>'DepartmentController@add'));
    Route::post('/department/add', array('as' => 'add', 'uses'=>'DepartmentController@add'));
});

});

我想在视图中显示成功提醒,但我没有在我的视图页面上获取会话值。请告诉我哪里出错?

1 个答案:

答案 0 :(得分:0)

确保所有路由都使用Web中间件

Route::group(['middleware' => 'web'], function () {
// routes here
Route::get('/departments', array('as' => 'departments',    'uses'=>'DepartmentController@index'));
});