2015-10-16 104 views
3

我正在学习如何使用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'); 
}); 

任何想法如何解决呢?

谢谢

+0

您应该意识到,在即将到来的第3版中,[正在删除Flash消息](http://www.slimframework.com/docs/start/upgrade.html#flash-messages)。 – alexw

+1

你是否以session_start开始会话? –

+0

是Gustavo,我正在使用session_start(); on index.php – Ashraam

回答

0

我还看到'flash'和'flashNow'方法在中间件中不起作用。要添加Flash信息,我决定手动完成。它正在工作,但我知道这不是最好的方法。

$_SESSION['slim.flash']['error'] = 'message'; 
0

自己遇到这个问题。

由于Flash是一个应用程序中间件组件,并默认添加,并且添加任何自定义应用中间件组件之前,实际上不会当你的中间件被称为初始化。

做什么@kkochanski所做的是哈克,但可能是唯一的选择,短删除/取消设置Flash和将其作为最终的应用中间件组件的。