0

I am making a project in Symfony 5 where there are multiple roles with different privileges. The problem is that when a user tries to log in a zone where they don't have enough privileges (e.g. a USER in an ADMIN only zone) it throws an Access Denied exception and I want to show a custom message instead of the Symfony error screen.

AccessDeniedException error

The security part has been done with the access_control method (in case that helps). I've tried following the docs related to this matter but I can't find the way to solve it.

Ben
  • 1

1 Answers1

1

If you want to customize the message and not change the response or the error code itself you can just customize the error template by creating a templates/bundles/TwigBundle/Exception/error403.html.twig template and clearing your caches.

The page won't be shown in the dev environment, though, because the default error page is more helpful during development. You can still preview it by accessing /_error/403.

msg
  • 7,863
  • 3
  • 14
  • 33
  • So the user will automatically see the custom error instead of the Symfony one? I don't have to change something else apart from what you've said? – Ben Aug 15 '22 at 07:53
  • Yes, but as I said it won't work in the `dev` environment, as the default debug error page is more useful in development; but it'll show in the `prod` environment and can be customized to use your site template. – msg Aug 15 '22 at 17:59