duanjiu2701 2016-03-29 02:03
浏览 50
已采纳

Laravel 5.2 Flash数据不存在

Should a redirect with flash data persist the flash data if the auth middleware is involved?

A few housekeeping things to note that will answer some possible followup questions:

    1. I am calling the web middleware.
    1. I'm using the file sessions driver.
    1. I can retrieve values stored in the session with the exception of flashed data.
    1. I have tried reflashing the flashed data by adding the following line to the Authenticate middleware:
      $request->session()->reflash();
      

As such, Authenticate.php now appears as follows:

public function handle($request, Closure $next, $guard = null)
{
    if (Auth::guard($guard)->guest()) {
        if ($request->ajax() || $request->wantsJson()) {
            return response('Unauthorized.', 401);
        } else {
            return redirect()->guest('login');
        }
    }

    $request->session()->reflash();

    return $next($request);
}

This issue is also affecting the auth boilerplate generated by make:auth, resulting in the $errors failing to display on error.

UPDATE (3/29 @ 08:54 EST)

I uncovered what I believe to have been the root cause, as you will see below. Each route was calling 'web' middleware twice. As such, two requests were actually taking place which was removing the flash message(s) before the user had a chance to see them. Original route:list is below.

+--------+----------+-------------------------+------+-----------------------------------------------------------------+---------------+
| Domain | Method   | URI                     | Name | Action                                                          | Middleware    |
+--------+----------+-------------------------+------+-----------------------------------------------------------------+---------------+
|        | GET|HEAD | /                       |      | Closure                                                         | web           |
|        | GET|HEAD | groups                  |      | App\Http\Controllers\GroupsController@index                     | web,web,auth  |
|        | GET|HEAD | groups/set-default/{id} |      | App\Http\Controllers\GroupsController@setDefaultGroup           | web,web,auth  |
|        | GET|HEAD | home                    |      | App\Http\Controllers\HomeController@index                       | web,web,auth  |
|        | GET|HEAD | login                   |      | App\Http\Controllers\Auth\AuthController@showLoginForm          | web,web,guest |
|        | POST     | login                   |      | App\Http\Controllers\Auth\AuthController@login                  | web,web,guest |
|        | GET|HEAD | logout                  |      | App\Http\Controllers\Auth\AuthController@logout                 | web,web       |
|        | POST     | password/email          |      | App\Http\Controllers\Auth\PasswordController@sendResetLinkEmail | web,web,guest |
|        | POST     | password/reset          |      | App\Http\Controllers\Auth\PasswordController@reset              | web,web,guest |
|        | GET|HEAD | password/reset/{token?} |      | App\Http\Controllers\Auth\PasswordController@showResetForm      | web,web,guest |
|        | GET|HEAD | register                |      | App\Http\Controllers\Auth\AuthController@showRegistrationForm   | web,web,guest |
|        | POST     | register                |      | App\Http\Controllers\Auth\AuthController@register               | web,web,guest |
|        | GET|HEAD | visitees                |      | App\Http\Controllers\VisiteesController@index                   | web,web,auth  |
|        | GET|HEAD | visitees/check-in/{id}  |      | App\Http\Controllers\VisiteesController@checkIn                 | web,web,auth  |
+--------+----------+-------------------------+------+-----------------------------------------------------------------+---------------+

My routes now appear as follows after removing the routes from the 'web' middleware:

+--------+----------+-------------------------+------+-----------------------------------------------------------------+------------+
| Domain | Method   | URI                     | Name | Action                                                          | Middleware |
+--------+----------+-------------------------+------+-----------------------------------------------------------------+------------+
|        | GET|HEAD | /                       |      | Closure                                                         | web        |
|        | GET|HEAD | groups                  |      | App\Http\Controllers\GroupsController@index                     | web,auth   |
|        | GET|HEAD | groups/set-default/{id} |      | App\Http\Controllers\GroupsController@setDefaultGroup           | web,auth   |
|        | GET|HEAD | home                    |      | App\Http\Controllers\HomeController@index                       | web,auth   |
|        | GET|HEAD | login                   |      | App\Http\Controllers\Auth\AuthController@showLoginForm          | web,guest  |
|        | POST     | login                   |      | App\Http\Controllers\Auth\AuthController@login                  | web,guest  |
|        | GET|HEAD | logout                  |      | App\Http\Controllers\Auth\AuthController@logout                 | web        |
|        | POST     | password/email          |      | App\Http\Controllers\Auth\PasswordController@sendResetLinkEmail | web,guest  |
|        | POST     | password/reset          |      | App\Http\Controllers\Auth\PasswordController@reset              | web,guest  |
|        | GET|HEAD | password/reset/{token?} |      | App\Http\Controllers\Auth\PasswordController@showResetForm      | web,guest  |
|        | GET|HEAD | register                |      | App\Http\Controllers\Auth\AuthController@showRegistrationForm   | web,guest  |
|        | POST     | register                |      | App\Http\Controllers\Auth\AuthController@register               | web,guest  |
|        | GET|HEAD | visitees                |      | App\Http\Controllers\VisiteesController@index                   | web,auth   |
|        | GET|HEAD | visitees/check-in/{id}  |      | App\Http\Controllers\VisiteesController@checkIn                 | web,auth   |
+--------+----------+-------------------------+------+-----------------------------------------------------------------+------------+

Upon moving the routes out of the 'web' middleware group, the flash message display correctly. But, now I have a new issue!

The flash messages are not being removed from the session after the initial request. They persist each subsequent request until they are manually flushed or forgotten.

I'm not sure at this point if I should open up a second question that specifically addresses the persisting of the flash data. Please advise if so.

  • 写回答

1条回答 默认 最新

  • 普通网友 2016-03-29 15:44
    关注

    please run a composer update to update laravel/framework to v5.2.27, then issue php artisan make:auth to regenerate auth routes

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。