Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
575 views
in Technique[技术] by (71.8m points)

symfony4 - Symfony 4.4 - Custom Error Templates Not Working

I've got an issue when it comes to using Custom Error Templates in Symfony 4.4 Flex. Everything has been set properly according to the guide from https://symfony.com/doc/4.4/controller/error_pages.html

I can access the Custom Error Templates via /_error/{errorCode} when I run the application with APP_ENV=dev - but if I change APP_ENV to prod (with APP_DEBUG set to false/0 of course), I get the default Symfony "500 Internal Server Error" page. I even tested it after deploying the application on the remote host, and it's the same issue when I try to access /random-unexisting-page (already got the error404.html.twig template set and it works on 'dev' via /_error/404).

I want to know, does it have anything to do with other configuration within /config/ ? Have you got any idea what the issue could be ?

question from:https://stackoverflow.com/questions/65851121/symfony-4-4-custom-error-templates-not-working

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I made a few tries and I think I've understood your problem.

So when you want to create a personalized error page, first you need to add, under the template directory, the following path, same as your link said

templates/
└─ bundles/
   └─ TwigBundle/
      └─ Exception/
         ├─ error404.html.twig
         ├─ error403.html.twig
         └─ error.html.twig      # All other HTML errors (including 500)

The most common error is the 500 so you can add this: error500.html.twig or just leave the generic one, it's up to you.

The importamt thing is that whenever you add a file error in the directory or you simply make a change in an existing file: you MUST launch the command to clear the cache (you can only launch this command when the project has no errors). If you don’t do this the changes wont be visualized.

STEPS:

  • fix the error or start from a previous working build
  • launch the clear command: php binconsole cache:clear, you'll get something like that result_of_clear_cache
  • refresh the page and you should see the new page

PS. if you need some visual references:

path

error_file

result_on_browser


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...