Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

declaring global variables in yii and use them in controller

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?
like image 408
The Georgia Avatar asked Apr 22 '14 16:04

The Georgia


People also ask

What is controller in Yii?

Controllers are part of the MVC architecture. They are objects of classes extending from yii\base\Controller and are responsible for processing requests and generating responses.

How do you declare global variables in pho?

Using global keyword. Using array GLOBALS[var_name]: It stores all global variables in an array called $GLOBALS[var_name]. Var_name is the name of the variable.

What are the different ways to declare global variables?

Declaring Variable: A variable can be either declared as a global or local variable. Variables can be declared by var, let, and const keywords.


2 Answers

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;
    }
}
like image 183
kachar Avatar answered Nov 15 '22 00:11

kachar


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'];
like image 35
Developerium Avatar answered Nov 14 '22 23:11

Developerium