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