2016-06-22 7 views
0

コントローラからmain.phpレイアウトファイルに変数値を渡そうとしていますが、エラーが発生します。未定義変数:contentWrapperBackground。以下の私のコードコントローラからyii2のレイアウトファイルに値を渡す方法

controller 
public function actionList() 
{ 

    $contentWrapperBackground = "ff4400;"; 
    $contentBackground = "3c8dbc;"; 
    return $this->render('list', [ 

     'contentWrapperBackground' =>$contentWrapperBackground, 
     'contentBackground' => $contentBackground, 

    ]); 
} 

であり、この

<div class="content-wrapper" style="background-color:#<?=$contentWrapperBackground?>"> 
    <!-- Content Header (Page header) --> 


    <!-- Main content --> 
    <section class="content" style="background-color:#<?=$contentBackground?>"> 

のように私のレイアウトファイルではなく、私は常にエラー未定義の変数を取得します:contentWrapperBackground。私は別のページの背景色を変更しようとしています。これについて助けてください、また、この仕事をどうお寄せいただきましたかについての別のアイディアにも開いています。

+0

可能な重複[YII2にレイアウトにコントローラからのparamを渡す方法]( http://stackoverflow.com/questions/28038912/how-to-pass-param-from-controller-to-layout-in-yii2) – soju

+0

sセッションとセッションの概念を取得する –

答えて

2

このためにセッションを使用しないでください!

シンプルなソリューション:

class Controller extends \yii\web\Controller 
{ 
    $public $contentWrapperBackground; 

    $public $contentBackground; 
} 

class YourController extends Controller 
{ 
    public function actionList() 
    { 

     $this->contentWrapperBackground = "ff4400;"; 
     $this->contentBackground = "3c8dbc;"; 
     return $this->render('list', []); 
    } 
} 

あなたの主なレイアウトで

<div class="content-wrapper" style="background-color:#<?=Yii::$app->controller->contentWrapperBackground?>"> 

または別のオプション

<div class="content-wrapper" style="background-color:#<?=$this->context->contentWrapperBackground?>"> 
関連する問題