Error Handling Yii 2.0

I have 2 goals that I am trying to accomplish

  1. Have a custom error page that shows a message when a user is logged in vs not logged in and trying to access a page that does not exist or that they don’t have rights too.

  2. Redirect all "fatal" errors to a custom location (db table / my log file / etc… ) so that it does not show up to the front end user. Instead they should see a standard error page with little to know info that I can control.

I believe I have goal 1 working to a degree of where I want. I can get the custom error page to show up for goal 1 and have it show different info based on if they are logged in or not. Goal 2 is the one giving me a problem. I can not get/understand how to stop the fatal error / stack trace from showing up on the front page.

I modified config/web so that the YII_ENV_DEV is set to production as shown on this page.

But I still get the fatal errors showing to the front end user. Also, I am not sure how I can override that default page so that it will show our custom message and then record that information to a place we would like to store it.

Any help would be appreciated and if you need more info from me please let me know.

I have tried modifying ./config/web.php to include a combination of the below lines

define('YII_ENV', 'prod');


$errorView = 'app/views/site/error.php';                                                        

$exceptionView = 'app/views/site/error.php'; 

and I still do not get the results I would except.

I am doing all this based off of what I see in

I am still having no luck getting this to work. Am I defining the variables in the wrong place or the wrong way?


Wrong place.

Delete what you have done in app/config/web.php

And do following in /app/web/index.php

// comment out the following two lines when deployed to production

// defined('YII_DEBUG') or define('YII_DEBUG', true);

// defined('YII_ENV') or define('YII_ENV', 'dev');

require(__DIR__ . '/../vendor/autoload.php');

require(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php');

$config = require(__DIR__ . '/../config/web.php');

(new yii\web\Application($config))->run();


Awesome MetaCrawler!

Thank you. That was it. I even did a grep in my /app/* looking for YII_ENV / YII_DEBUG and I didn’t find it. I must have fat fingered it or something.

Also the key here is that the documentation was saying to modify an entry script which I don’t think the config/web.php file is. I didn’t catch that until this all started working.

Glad I could help.

Nice weekend. ;)