declaring global variables in yii and use them in controller

26,536

Solution 1

You can use the class variables to achieve that, here's an example:

<?php

class ApiController extends Controller
{
    public $lang = 'en';

    public function beforeAction($action)
    {
        if(Yii::app()->session->contains('lang'))
            $this->lang = Yii::app()->session['lang'];
        return parent::beforeAction($action);
    }

    public function afterAction($action,$params)
    {
        Yii::app()->session['lang'] = $this->lang;
        return parent::afterAction($action,$params);
    }

    public function actionDisplay(){
        echo 'In Display action';
        $this->lang = 'test'
        echo $this->lang;
    }

    public function actionDisplay2(){
        echo 'In Display2 action';
        echo $this->lang;
    }
}

Solution 2

you can make static system variables, here is a guide on that

basicaly in config/main.php you have to add

...
'params' => array(
    'email'      => '[email protected]',
    'someOption' => true
),
...

then you can use it like

$email = Yii::app()->params['email'];

Solution 3

You wrote that you created variable $login = 'false' So 'false' there is a string. And it's not empty string so $login == true. Just change it to

'params'=>array('login' => false),

Update: About this feature, maybe you should to create helper class with static field:

class SomeHelper
{
     //that field can be changed and used in any place
     public $static $login = false;
}
Share:
26,536
The Georgia
Author by

The Georgia

Updated on December 19, 2020

Comments

  • The Georgia
    The Georgia almost 3 years

    I have been trying to declare a global variable in yii that is a boolean, and have its value changed in different action functions in the controller. Below is an example of what i am trying to achieve.

    in .../config/main.php i added the following array: 'params'=>array('login' => 'true',),

    in .../protected/controller/testController.php i added the following code:

    <?php
        class ApiController extends Controller{
    public $x = '';
    
    public function actionDisplay(){
    $x=Yii::app()->params['login']; //so x now has the value "true"
    echo $x; //this display "true" when i run this controller on this function
    }
    
    public function actionDisplay2(){
    global $x;
        echo $x; //this for some reason does not contain the value true even if x is global
    }
    

    How can i achieve this without having to assign a value in each function to the global variable? If i call the second function, it throws an error that x is not defined. My plan is to use the global variable the way you do in java e.g.

    public class Display{
    
    public String x = " ";
    
    public static void Display(){
    x = "True"; //global variable x is assigned the String value "True"
    }
    
    public static void DisplayTwo(){
    System.out.print("Value of x is: " + x); //this will print "Value of x is: True"
    }
    
    ....
    
    }
    So basically, this is how i want to use the global variable in Yii framework. Any suggestions how to achieve this please?
    
  • The Georgia
    The Georgia over 9 years
    Alex, that was a typo.$login = 'true' in the 'params' array. But that is still not the problem. Like i said, i am trying to use the global variable in different functions while assigning the value to it once just like demonstrated in my second example referencing Java.
  • The Georgia
    The Georgia over 9 years
    Tinybyte. This works. But now how can i change the value for email in a function located in the controller so that other functions that use $email = Yii::app()->params['email'] will not get the new value for email instead of the original value assigned in the 'params' array in the main.php file? For example, if i change $email = Yii::app()->params['email']="[email protected] in some function in the controller, and i want other functions using $email = Yii::app()->params['email'] to now get the newly assigned email of [email protected].
  • The Georgia
    The Georgia over 9 years
    Alex, $login = 'true' in the 'params' array. But that is still not the problem. How can i change the value for email in a function located in the controller so that other functions that use $email = Yii::app()->params['email'] will not get the new value for email instead of the original value assigned in the 'params' array in the main.php file? For example, if i change $email = Yii::app()->params['email']="[email protected] in some function in the controller, and i want other functions using $email = Yii::app()->params['email'] to now get the newly assigned email of [email protected].
  • The Georgia
    The Georgia over 9 years
    Nazim, this does not solve the problem. Please heck my comment below
  • Developerium
    Developerium over 9 years
    like I said, these are static parameters, if you want to be able to change these variables, you need to store that variable in database or cache or something similar, this might help : yiiframework.com/extension/settings
  • The Georgia
    The Georgia over 9 years
    Is it possible to change the "param" array value in the config main.php file from a function in the controller?
  • The Georgia
    The Georgia over 9 years
    This works if you just want to display the original value. But what if i want to change the value of login in actionDisplay e.g $this->lang = Yii::app()->params['login'] = "hello"; Then if i echo $this->lang in Display2, it should display "Hello". That is what i am trying to accomplish.
  • kachar
    kachar over 9 years
    You have to use the session. Display and Display2 actions does not run on the same server request but on different. You have to store somewhere the value and then gather it back. In Display you can save with Yii::app()->session['login'] = 'hello'; and in Display2 gather it by Yii::app()->session['login']. One server request runs only one action* from the class.
  • The Georgia
    The Georgia over 9 years
    Thanks for the help @Kacher. Your solution plus suggestions helped. Got it working now.
  • Rich Harding
    Rich Harding almost 3 years
    You don't have to use the session variable. This ApiController extends Controller. Then you set your SiteController (or whatever - the one containing your relevant actions) to extend ApiController instead of Controller, which makes ApiController's public variables read/write in SiteController.