我正在学习如何使用Slim框架,并且我在使用自定义中间件内的Flash消息时遇到了问题。超薄框架闪存和中间件
问题很简单:我在中间件中使用了$app->flash('error', 'my message');
,下一页中没有消息。
中间件的方式
这里的伟大工程,是我的代码:
中间件
class SessionMiddleware extends \Slim\Middleware {
public function call() {
$app = $this->app;
$req = $app->request;
if(!preg_match('#^(/public)#', $req->getPath())) {
if(isset($_SESSION['user']) && !empty($_SESSION['user'])) {
//Do something
} else {
$app->flash('error', 'You must be logged');
if($req->getPath() != '/login') {
$app->redirect('/login');
}
}
}
$this->next->call();
}
}
登录
<?php
if(isset($_SESSION['slim.flash']['error'])) {
echo '<p class="alert alert-danger"><strong>Error</strong>: '.$_SESSION['slim.flash']['error'].'</p>';
}
?>
应用
$app = new \Slim\Slim(array(
'mode' => 'development',
'debug' => true,
'templates.path' => './templates'
));
$app->add(new \SessionMiddleware());
$app->get('/login', function() use($app) {
$app->render('login.php');
});
任何想法如何解决呢?
谢谢
您应该意识到,在即将到来的第3版中,[正在删除Flash消息](http://www.slimframework.com/docs/start/upgrade.html#flash-messages)。 – alexw
你是否以session_start开始会话? –
是Gustavo,我正在使用session_start(); on index.php – Ashraam