TheSmile
TheSmile

Reputation: 360

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

Also tried declare in Controller public $ERROR_USERNAME_INVALID = "ERROR_USERNAME_INVALID"; and echo in View ... still Undefined variable: ERROR_USERNAME_INVALID

Any Suggestion to do pass variable to view ? Thanks

Upvotes: 2

Views: 1534

Answers (1)

Yatin Mistry
Yatin Mistry

Reputation: 1264

This trick worked

The value given in if condition is treated as constant variable. so if not worked So you have to add pass it as a string blablabla to "blablabla"

Otherwise

1==1 or any valid condition

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;

Upvotes: 1

Related Questions