2016-06-22 4 views
0

컨트롤러에서 main.php 레이아웃 파일로 변수 값을 전달하려고하는데 오류가 계속 발생합니다. Undefined variable : 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

사용 et 세션 및 세션 개념을 얻으십시오. –

답변

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?>"> 
관련 문제