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

Slim 3 - Flash message not showing up after redirecting

我在创建 remember me 功能时遇到了 slim flash 的问题。这是我的路线

/* 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,尝试访问主页或登录页面,闪现消息原样显示。好

  2. 尝试在不删除 PHPSESSID 的情况下访问登录页面。它重定向到主页。好

  3. 在第 2 次之后,我删除了 PHPSESSID,然后尝试再次访问我的登录页面,然后闪现消息没有正常显示。

我不知道为什么当我尝试访问主页时(删除 PHPSESSID 后)在数字 (3) 之后只显示闪现消息,但当我尝试访问登录页面时却没有。

我只是想出了一个简单的解决方案,但不确定它是否足够好。我只是重定向到另一个 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'));
  }
}

有了这个,闪信总是出现。