简体   繁体   English

Yii从控制器传递变量以查看不起作用

[英]Yii pass variable from controller to view not working

I want to pass variable from controller to view but it doesn't working ... 我想将变量从控制器传递到视图,但是它不起作用...

In Controller 在控制器中

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

}

In View 视野中

<?php echo $ERROR_USERNAME_INVALID; ?>

Error 错误

Undefined variable: ERROR_USERNAME_INVALID 未定义的变量:ERROR_USERNAME_INVALID

Also tried declare in Controller public $ERROR_USERNAME_INVALID = "ERROR_USERNAME_INVALID"; 还尝试在Controller public $ERROR_USERNAME_INVALID = "ERROR_USERNAME_INVALID"; and echo in View ... still Undefined variable: ERROR_USERNAME_INVALID 并在View中echo ...仍未定义变量:ERROR_USERNAME_INVALID

Any Suggestion to do pass variable to view ? 任何建议做传递变量查看? Thanks 谢谢

This trick worked 这个技巧奏效了

The value given in if condition is treated as constant variable. if条件中给出的值被视为常量。 so if not worked So you have to add pass it as a string blablabla to "blablabla" 因此,如果不起作用,那么您必须将其作为字符串blablabla添加到"blablabla"

Otherwise 除此以外

1==1 or any valid condition 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));
    }
}

Or Try like below 或者尝试如下

In Controller 在控制器中

public function actionSignIn(){

       $my_var = "My Custom Value";

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

In view 视野中

echo $my_var;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM