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


Yii pass variable from controller to view not working

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

在控制器中

    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

有没有建议将变量传递给视图?感谢

这个技巧对有效

if条件中给定的值被视为常量变量。所以如果不起作用所以你必须添加pass作为字符串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;