Slim 3-重定向后未显示Flash消息

时间:2018-10-02 06:39:50

标签: php slim

在创建remember me功能时,我在超薄闪存上遇到了这个问题。这是我的路线

/* authenticated */
$app->group('', function () {
  $this->get('/', 'HomeController:index')->setName('home');
})->add(new \App\Middleware\HomeMiddleware($container));

/* public */
$app->->group('', function () {
  $this->get('/login', 'AuthController:getLogin')->setName('get.login');
  $this->post('/login', 'AuthController:postLogin');
})->add(new \App\Middleware\PublicMiddleware($container));

在我的HomeMiddleware

Class HomeMiddleware extends Middleware
{
  public function __invoke($request, $response, $next)
  {
    if (!Session::exists('u_session') && !Cookie::exists('user')) {
      return $response->withRedirect($this->router->pathFor('get.login'));
    }

    if (!Session::exists('u_session') && Cookie::exists('user')) {
      $hash = Cookie::get('user');

      $ucookie = new UCookie;
      $check = $ucookie->where('hash', $hash)->first();

      if (!$check) {
        Cookie::delete('user');
        return $response->withRedirect($this->router->pathFor('get.login'));
      }

      $newhash = bin2hex(random_bytes(32));

      Cookie::set('user', $newhash);

      $ucookie->hash = $newhash;
      $ucookie->save();

      Session::set('u_session', $ucookie->user_id);

      $this->flash->addMessage('success', 'Welcome back!');
      return $response->withRedirect($this->router->pathFor('home'));
    }

    return $next($request, $response);
  }
}

这是我的PublicMiddleware

class PublicMiddleware extends Middleware
{
  public function __invoke($request, $response, $next)
  {
    if (Session::exists('u_session') || Cookie::exists('user')) {
      return $response->withRedirect($this->router->pathFor('home'));
    }

    return $next($request, $response);
  }
}
  1. 删除PHPSESSID并尝试访问主页或登录页面,Flash消息按原样显示。好

  2. 尝试访问登录页面,而不删除PHPSESSID。它重定向到主页。好

  3. 在第2个数字之后,我删除了PHPSESSID,然后尝试再次访问我的登录页面,然后Flash消息没有按预期显示。

我不知道为什么在尝试访问主页(删除PHPSESSID后)后仅显示第(3)条Flash消息,但是在尝试访问登录页面时却不显示。

1 个答案:

答案 0 :(得分:0)

我只是用简单的解决方案弄清楚了,但不确定它是否足够好。我只是重定向到另一个authenticated route并处理其中的闪烁

Class HomeMiddleware extends Middleware
{
  public function __invoke($request, $response, $next)
  {
    if (!Session::exists('u_session') && !Cookie::exists('user')) {
      return $response->withRedirect($this->router->pathFor('get.login'));
    }

    if (!Session::exists('u_session') && Cookie::exists('user')) {

      $data = explode('_', Cookie::get('user'));

      if (!empty($data) && count($data) === 2) {
        $token = $data[0];
        $hash = $data[1];

        $ucookie = UCookie::where('token', $token)->first();

        if (!$ucookie || !password_verify($hash, $ucookie->hash)) {
          Cookie::delete('user');
          return $response->withRedirect($this->router->pathFor('get.login'));
        }

        $newtoken = bin2hex(random_bytes(32));
        $newhash = bin2hex(random_bytes(32));

        $ucookie->token = $newtoken;
        $ucookie->hash = password_hash($newhash, PASSWORD_DEFAULT);
        $ucookie->save();

        Cookie::set('user', $newtoken . '_' . $newhash);
        Session::set('u_session', $ucookie->user_id);

        return $response->withRedirect($this->router->pathFor('flash.success'));
      }
    }

    return $next($request, $response);
  }
}

并创建新的经过身份验证的路由,该路由可访问,因为我已经创建了会话登录名

$app->group('', function () {

  $this->get('/flash-success', 'FlashController:flashSuccess')->setName('flash.success');

})->add(new \App\Middleware\HomeMiddleware($container));

在我的FlashController内只需添加新方法

class FlashController extends Controller
{
  public function flashSuccess($request, $response)
  {
    $this->flash->addMessage('success', 'Welcome back!');
    return $response->withRedirect($this->router->pathFor('home'));
  }
}

以此方式,总是显示Flash消息。