2016-06-08 40 views
0

對於設置錯誤動作我在控制器中添加該代碼設置錯誤動作佈局不工作

public function beforeAction($action) { 
    if ($action->id == 'error') 
     $this->layout = 'iframe-main.php'; 

    $this->enableCsrfValidation = false; 
    return parent::beforeAction($action); 
} 

但它不是working.Error佈局在默認佈局顯示

+0

類似的問題在這裏:http://stackoverflow.com/questions/27573826/how-to-set-layout-for-errorhandler-dynamically-without-module – robsch

回答

2

試試這個:

public function beforeAction($action) { 
    if (parent::beforeAction($action)) { 
     // change layout for error action 
     if ($action->id=='error') $this->layout ='iframe-main'; 
     return true; 
    } else { 
     return false; 
    } 
} 
+0

不工作! @jithin – Jackhad

+0

ahh ..我的錯誤..我添加這個在我的控制器,而不是sitecontroller.php.Working罰款!謝謝!! :) – Jackhad

0

在你actionError()SiteController的只需添加默認佈局名稱

class SiteController extends Controller 
{ 

public function actionError() 
    { 
     $this->layout='defaultLayoutName'; 
      //rest of the code goes here 
    } 
} 
+0

即時通訊使用Yii2.There不會有actionError() – Jackhad

1

添加到您的配置:

'components' => ['errorHandler' => [ 
     'errorAction' => 'site/error', 
    ], 

創建控制器如果不存在:SiteController.php與內容:

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]); 
     } 
    } 
} 

,最簡單的瀏覽網站/ error.php:

<?php 
    use yii\helpers\Html; 
?> 
<div class="site-error"> 
     <?= Html::encode($exception->getMessage()) ?> 
</div> 

在Yii2上測試。在文檔中的更多信息http://www.yiiframework.com/doc-2.0/guide-runtime-handling-errors.html#using-error-handler