dongshi1215 2014-03-31 08:04
浏览 35
已采纳

Silex(Symfony2微框架)在bootstrap.php中重定向

I would like to create a redirect when the user session expires. I know how to do this like so:

$app->before(function (Request $request) {
    if (!$request->getSession()->get('displayName')) {
        return new RedirectResponse('login', 301);
    }
});

The problem is that I get the error : Fatal error: Class 'RedirectResponse' not found in /Applications/MAMP/htdocs/pst/app/bootstrap.php on line 120

I also tried to add this in the if statement:

$app->get('/', function () use ($app) {
   return $app->redirect('/hello');
});

but the app variable isn't defined. What is the right way to do this?

UPDATE:
I added the RedirectResponse function from Symfony2. But the displayName isn't correct. I dumped by session, could anyone take a look of what it should be?
DUMP: http://pastebin.com/7XQbJJ08

In my layout.twig (general layout) I have <span>&nbsp;&nbsp;{{ app.user.displayName }}</span> and after a while no reaction he says he can't find the attribute displayName on a null attribute.

  • 写回答

1条回答 默认 最新

  • dongxie2749 2014-03-31 08:11
    关注

    For the first approach you'll need to make sure you've imported the RedirectResponse first. You can do this by adding the following to the top of your script:

    use \Symfony\Component\HttpFoundation\RedirectResponse;
    

    If this still doesn't work then you may have autoloader problems.

    For the second case I'm not sure how $app could be undedfined. As this will have been created when you called:

    $app = new Silex\Application();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题