7

I know this is a big NO NO... and should not be displaying developer error pages while my site is live, what can I do to ensure dev environment error messages are not appearing in production?

enter image description here

Why are these appearing? I thought it was turned off by default when in production mode? Did I miss a setting?

Note: This is on a shared server. And am using the app.php not app_dev.php.

When I go to production mode locally it properly displays the right error messages (below):

Oops! An Error Occurred The server returned a "404 Not Found". Something is broken. Please e-mail us at [email] and let us know what you were doing when this error occurred. We will fix it as soon as possible. Sorry for any inconvenience caused.

Yet on the live site it's showing the Symfony2 dev environment error message?

I've tried to make a custom error message by making a error404.html.twig file in app/Resource/TwigBundle/views/Exception but it still doesn't load this file and just displays the developer error message.

likeitlikeit
  • 5,563
  • 5
  • 42
  • 56
dizzyd
  • 201
  • 1
  • 7
  • 15
  • Are you actually using Symfony's 'dev' environment? Note that there are two front controllers in the `web` folder, `app.php` and `app_dev.php`. Which one are you using? – Nic Wortel Apr 28 '14 at 08:30
  • Using app.php. It won't even let me use app_dev.php, when I try I get this error message "You are not allowed to access this file. Check app_dev.php for more information." – dizzyd Apr 28 '14 at 08:35
  • In app.php - $kernel = new AppKernel('prod', true); – dizzyd Apr 28 '14 at 08:36
  • That's probably causing your problem, see my answer. – Nic Wortel Apr 28 '14 at 08:46

1 Answers1

17

In your frontend controller (web/app.php in the Symfony Standard Edition), an instance of AppKernel is created. AppKernel inherits the constructor from Symfony's Kernel, which requires two arguments:

/**
 * Constructor.
 *
 * @param string $environment The environment
 * @param bool   $debug       Whether to enable debugging or not
 */
public function __construct($environment, $debug)

The $environment parameter only determines which configuration is used (config_dev.yml, config_prod.yml, etc.). The $debug parameter is the one that enables or disables debugging (and therefore determines wether exceptions are shown or not).

So in app.php, change:

$kernel = new AppKernel('prod', true);

to

$kernel = new AppKernel('prod', false);

This should replace the detailed exception pages with user-friendly error pages.

Nic Wortel
  • 11,155
  • 6
  • 60
  • 79
  • That worked! Where do I find $debug is set to true at? I was following a guide than mentioned to turn `'prod', true`. Is this is in app_dev.php where `'dev', true`? – dizzyd Apr 28 '14 at 08:48
  • What do you mean? You set `$debug` to true when you create a new instance (`$kernel = new AppKernel('prod', true);`). The constructor is defined in `vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Kernel.php` at line 77. – Nic Wortel Apr 28 '14 at 08:50
  • Doesn't `$kernel = new AppKernel('prod', true);` mean you're setting it to production mode? This is where I'm getting confused. I was following this guide here: http://www.livelywebdesign.com/blog/2013/05/symfony-2-application-on-shared-hosting/#comment-52147 – dizzyd Apr 28 '14 at 08:53
  • And in it they mentioned, "Lastly, we need to change $kernel = new AppKernel('prod', false); Change the second argument in the instantiation of the AppKernal object from 'false' to 'true'." – dizzyd Apr 28 '14 at 08:53
  • Yes, that's true, the first parameter (prod/dev/...) sets the environment. However, the second parameter sets the debug mode (true or false). The environment only determines which configuration is used, not wether or not Exceptions are shown. (I learned something new today, too) See Symfony's doc: http://symfony.com/doc/current/cookbook/configuration/environments.html – Nic Wortel Apr 28 '14 at 08:54
  • I don't know why this guide is telling you to enable debug mode in production, but it doesn't really sound like a good idea. Can you get your site working without debug turned on? If you can, I'd suggest you leave it off... – Nic Wortel Apr 28 '14 at 08:59
  • Thank you brother, I can't argue with the official documentation and I'll take that as the gospel over that guide. I'll leave the other app_dev.php settings as is as I haven't touched it. – dizzyd Apr 28 '14 at 09:07
  • Thanks Nic Wortel. Your point is good to see error in production environment. – Bhavin Thummar Mar 28 '18 at 06:03