我正在编写会话中间件,用于检查用户是否已登录并拥有会话。这是简化的中间件类
的index.php
// To help the built-in PHP dev server, check if the request was actually for
// something which should probably be served as a static file
if (PHP_SAPI === 'cli-server' && $_SERVER['SCRIPT_FILENAME'] !== __FILE__) {
return false;
}
require __DIR__ . '/../vendor/autoload.php';
session_start();
// Instantiate the app
$settings = require __DIR__ . '/../app/settings.php';
$app = new \Slim\App($settings);
// Set up dependencies
require __DIR__ . '/../app/dependencies.php';
// Register middleware
require __DIR__ . '/../app/middleware.php';
// Register routes
require __DIR__ . '/../app/routes.php';
// Run!
$app->run();
middleware.php
<?php
// Application middleware
$app->add( new App\Middlewares\sessionMiddleWare($container) );
和App \ Middlewares \ sessionMiddleWare
<?php
namespace App\Middlewares;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use \PDO;
private $database;
private $settings;
class sessionMiddleWare {
public function __construct($container){
$this->database = $container['databaselayer'];
$this->settings = $container['settings'];
}
public function __invoke(RequestInterface $request, ResponseInterface $response, $next){
if(!$_SESSION['util']){
// Do some stuff (create session variable and set idPageWeb he wants into $this->settings['idPageWeb']
}
//then
if( $_SESSION["idUtil"] ){
$response = $next($request->withAttribute("idPageWeb",$this->settings["idPageWeb"]), $response);
}else{
//forbiden
}
}
}
我的问题是第一次加载,这个&gt;设置['idPageWeb']设置正确,但是如果我重新加载,我已经有会话,所以通过下一个但是$ this-&gt; setting ['idPageWeb']是空的。所以我的问题是如何全局设置这个var?或者将var保存到应用程序设置中?
请求帮助。