How to do a redirect in your exception handler?

/*
When I go to the page /login_form, the exception Twig_Error_Runtime exception interrupts my AlreadyLoginException how to suppress Twig_Error_Runtime?
*/

EDIT: How to do a redirect in your exception handler? the exception is thrown from a controller which is rendered in Twigg-template. In dev environment the redirect occurs in the prod environment controller is just not being rendered in the template.

Code only as an example:
Symfony 3.4
namespace AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use AppBundle\Exception\AlreadyLoginException;

class DefaultController extends Controller
{
/**
 * @Route("/", name="homepage")
*/
 public function indexAction(Request $request)
{
 return $this->render('default/index.html.twig', [
 'base_dir' => realpath($this->getParameter('kernel.project_dir')).DIRECTORY_SEPARATOR,
]);
}

/**
 * @Route("/login", name="login")
*/
 public function loginAction(Request $request)
{
 if ($this->getUser()) {
 throw new AlreadyLoginException('This user does not have access to this section.');
}

 return $this->render('default/login.html.twig', []);
}

/**
 * @Route("/login_form", name="login_form")
*/
 public function loginFormAction(Request $request)
{
 return $this->render('default/login_form.html.twig', []);
}
}

namespace AppBundle\EventListener;

use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpFoundation\RedirectResponse;
use AppBundle\Exception\AlreadyLoginException;
use Symfony\Component\Routing\RouterInterface;

class AlreadyLoginExceptionListener
{
 private $router;

 public function __construct(RouterInterface $router)
{
 $this->router = $router;
}

 public function onKernelException(GetResponseForExceptionEvent $event)
{
 if ((!$event->getException()->getPrevious() instanceof AlreadyLoginException)
 && (!$event->getException() instanceof AlreadyLoginException)) {
return;
 } //@voronkovich thx 

 $response = new RedirectResponse($this->router->generate('homepage'));
$event->setResponse($response);
}

}

services:
kernel.listener.alreadyloginexception:
 class: AppBundle\EventListener\AlreadyLoginExceptionListener
arguments:
 - "@router"
tags:
 - { name: kernel.event_listener, event: kernel.exception }

{# login_form.html.twig #}
{{ render(controller('AppBundle:Default:login')) }}
June 10th 19 at 14:46
2 answers
June 10th 19 at 14:48
I believe that Twig is wrapped exception occurs. To obtain a copy of the original exception, use the method getPrevious: php.net/manual/en/class.exception.php.

if (!$event->getException()->getPrevious() instanceof AlreadyLoginException) {
return;
}
Thank you, it seems to work - nikita.Stracke commented on June 10th 19 at 14:51
Hurry in dev environment all works fine, in prod'e just not rendered by a controller in which there is an exception - nikita.Stracke commented on June 10th 19 at 14:54
And as you want? A function cannot return a value and throw an exception. What to render if an exception be thrown? - Darien46 commented on June 10th 19 at 14:57
I don't need to render, I need to do a redirect - nikita.Stracke commented on June 10th 19 at 15:00
You wrote that:

In dev environment the redirect occurs in the prod environment controller is just not being rendered in the template
.

What is enachit: "not being rendered in the template"?
In my opinion, you in vain use a subquery because with debug=false (prod) it works differently, returning an empty string in case of error: FragmentHandler.php#L62.

You need it:
public function loginAction(Request $request)
{
 if ($this->getUser()) {
 return $this->redirectToRoute('homepage');
}

 return $this->render('default/login.html.twig', []);
}
- Darien46 commented on June 10th 19 at 15:03
And get the error when entering the /login_form - nikita.Stracke commented on June 10th 19 at 15:06
What mistake? - Darien46 commented on June 10th 19 at 15:09
An exception has been thrown during the rendering of a template ("Error when rendering "http://localhost/login_form" (Status code is 302).").
- nikita.Stracke commented on June 10th 19 at 15:12
So you remove the subquery, just the form code there to paste. If I understand correctly, you want user, already authenticated, to redirect to the main page? - Darien46 commented on June 10th 19 at 15:15
I have written code only as an example - nikita.Stracke commented on June 10th 19 at 15:18
Your example will not work and it cannot be healthy because you can't do a redirect from the subquery. Look at this line, you will see that Symfony will just take the content of the request, but will not give up the request itself. Redirect doesn't work. - Darien46 commented on June 10th 19 at 15:21
June 10th 19 at 14:50
Your task is not to let authentifizierung juzverej?

Tale abstract Security anonymous juzverej or security.yml to indicate that the page entry is just anonymous.

Better yet, make a redirect from this page.

The whole solution is at least 1 line in the configuration or 2 in a particular controller.

PS.
Entrance authentifizierung juzverej on the page for anonymous VA should not be the exclusive situation of your business logic. Reverse - Yes. But for you it makes a framework by redirecting to login_path juzverej
This code only as an example - nikita.Stracke commented on June 10th 19 at 14:53

Find more questions by tags SymfonyTwig