For setting error action I added this code in my controller
public function beforeAction($action) {
    if ($action->id == 'error')
        $this->layout = 'iframe-main.php';
    $this->enableCsrfValidation = false;
    return parent::beforeAction($action);
}
But its not working.Error layout is displaying in default layout
You could use Yii2 official yii\web\ErrorAction to handle error in controller:
/**
 * {@inheritdoc}
 */
public function actions()
{
    return [
        'error' => [
            'class' => 'yii\web\ErrorAction',
            'layout' => 'login',
        ],
    ];
}
Note that we could set the layout property for changing layout for error view.
https://www.yiiframework.com/doc/api/2.0/yii-web-erroraction
Add to your config:
'components' => ['errorHandler' => [
        'errorAction' => 'site/error',
    ],
Create controller if not exists: SiteController.php with content:
namespace app\controllers;
use Yii;
use yii\web\Controller;
class SiteController extends Controller
{
    public function actionError()
    {
        $exception = Yii::$app->errorHandler->exception;
        if ($exception !== null) {
            $this->layout = 'yourNewLayout';
            return $this->render('error', ['exception' => $exception]);
        }
    }
}
And simplest view site/error.php:
<?php 
    use yii\helpers\Html; 
?>
<div class="site-error">
        <?= Html::encode($exception->getMessage()) ?>
</div>
Tested on Yii2. More information in documentation http://www.yiiframework.com/doc-2.0/guide-runtime-handling-errors.html#using-error-handler
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With