Silex (Symfony2 micro framework) redirect in 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:

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.


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();

Need Your Help

onProgress loading for textures with Three.js

javascript three.js

Hi I am having some trouble generating a loading message when loading a texture for a scene. the onProgress console.log message is only fired when the texture has downloaded completely and not duri...