I want to implement logic that shows a custom 404 error page on a Symfony environment when there's no route found and a NotFoundHttpException has been thrown.
I've followed the cookbook solution line by line, also number of other sources, including StackOverflow and even trying to handle it via listener and I just can't make it work.
No matter what url I load on stage/prod it always returns 500 Server error. I've added several error pages in:
/app/Resources/TwigBundle/views/Exception/
- error.html.twig
- error404.html.twig
- error500.html.twig
- exception.html.twig
- exception404.html.twig
- exception500.html.twig
just to try and catch both errors and/or exceptions but there's no change. Still Server error 500 shown when trying to load http://mystage.env/non-existing-url
. It never really loads any error/exception content.
The app_stage.php and app_prod.php both use false on debugger
$kernel = new AppKernel('stage', false);
I have a feeling I am missing some obvious configuration property in my parameters.yml or routing.yml or both, but I have no idea what. So far in my config.yml file part of the Twig configuration is:
# Twig Configuration
twig:
exception_controller: twig.controller.exception:showAction
debug: "%kernel.debug%"
When I dump the template, that needs to be loaded here:
# vendor/symfony/symfony/src/Symfony/Bundle/TwigBundle/Controller/ExceptionController.php
public function showAction(
Request $request,
FlattenException $exception,
DebugLoggerInterface $logger = null
)
{
var_dump((string) $this->findTemplate(
$request,
$request->getRequestFormat(),
$code,
$this->debug
)); die;
#...
}
it shows the correct and expected template: string 'TwigBundle:Exception:error404.html.twig'
Yet it doesn't load it, but returns 500 Server error.
For the record, I also tried using the WebfactoryExceptionsBundle, but the result was the same.
EDIT
I removed the Twig configuration line for the exception_controller, as suggested in the comments. This is what the app/logs/stage.log says:
request.ERROR:
Uncaught PHP Exception Symfony\Component\HttpKernel\Exception\NotFoundHttpException:
"No route found for "GET /nonexisting"" at
/var/www/html/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/EventListener/RouterListener.php line 145
{
"exception":"[object] (
Symfony\\Component\\HttpKernel\\Exception\\NotFoundHttpException(code: 0):
No route found for \"GET /nonexisting\" at
/var/www/html/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/EventListener/RouterListener.php:145,
Symfony\\Component\\Routing\\Exception\\ResourceNotFoundException(code: 0): at
/var/www/html/app/cache/stage/appStageUrlMatcher.php:1018
)"
} []
request.ERROR:
Exception thrown when handling an exception (
Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException:
The security context contains no authentication token.
One possible reason may be that there is no firewall configured for this URL.
)
{
"exception":"[object] (
Symfony\\Component\\HttpKernel\\Exception\\NotFoundHttpException(code: 0):
No route found for \"GET /nonexisting\" at
/var/www/html/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/EventListener/RouterListener.php:145,
Symfony\\Component\\Routing\\Exception\\ResourceNotFoundException(code: 0): at
/var/www/html/app/cache/stage/appStageUrlMatcher.php:1018
)"
} []
Same errors are logged whether I'm successfully logged or not.