Yii从控制器传递变量到视图不工作

时间:2014-10-31 03:12:54

标签: php yii

我想将变量从控制器传递到视图,但它不起作用......

在控制器中

    class RegisterController extends Controller
{
    public function actionRegisterPage() 
    {
        // This function I have put it on url manager.
        // And I tried at here worked fine
        $util = new Utility();              
        $util->detectMobileBrowser();   
        $util->checkWebSiteLanguageInCookies(); 
        $this->layout = "masterLayout";
        $this->render('index');
    }
    public function actionSignIn(){

        if(blablabla){
            // Here is for me to display certain message, so I pass wish to pass $ERROR_USERNAME_INVALID to View
           $ERROR_USERNAME_INVALID = "ERROR_USERNAME_INVALID";

           $this->render('index',array('ERROR_USERNAME_INVALID'=>$ERROR_USERNAME_INVALID));
        }
    }

}

在视图中

<?php echo $ERROR_USERNAME_INVALID; ?>

错误

未定义的变量:ERROR_USERNAME_INVALID

还尝试在视图中的控制器public $ERROR_USERNAME_INVALID = "ERROR_USERNAME_INVALID";echo中声明...仍然未定义的变量:ERROR_USERNAME_INVALID

任何建议传递变量来查看?感谢

1 个答案:

答案 0 :(得分:1)

这个技巧有效

if条件中给出的值被视为常量变量。所以,如果没有工作 所以你必须添加传递它作为一个字符串 blablabla到"blablabla"

否则

1==1或任何有效条件

public function actionSignIn(){

    if("blablabla"){   // Write value as string ("" in double quotes) other remove condition if not required

       $ERROR_USERNAME_INVALID = "ERROR_USERNAME_INVALID";

       $this->render('index',array('ERROR_USERNAME_INVALID'=>$ERROR_USERNAME_INVALID));
    }
}

或尝试如下

在控制器

public function actionSignIn(){

       $my_var = "My Custom Value";

       $this->render('index',array('my_var'=>$my_var));
    }
}

在视图中

echo $my_var;