php 在 yii 中声明全局变量并在控制器中使用它们
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23225685/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
declaring global variables in yii and use them in controller
提问by The Georgia
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.
我一直试图在 yii 中声明一个布尔值的全局变量,并在控制器的不同动作函数中更改其值。下面是我试图实现的一个例子。
in .../config/main.php i added the following array: 'params'=>array('login' => 'true',),
在 .../config/main.php 我添加了以下数组:'params'=>array('login' => 'true',),
in .../protected/controller/testController.php i added the following code:
在 .../protected/controller/testController.php 我添加了以下代码:
<?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.
我怎样才能做到这一点,而不必在每个函数中为全局变量分配一个值?如果我调用第二个函数,它会抛出一个错误,指出 x 未定义。我的计划是像在 java 中那样使用全局变量,例如
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?
采纳答案by kachar
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;
}
}
回答by Developerium
you can make staticsystem variables, here is a guideon that
可以使静态的系统变量,这里是一个引导上
basicaly in config/main.php
you have to add
基本上config/main.php
你必须添加
...
'params' => array(
'email' => '[email protected]',
'someOption' => true
),
...
then you can use it like
然后你可以像这样使用它
$email = Yii::app()->params['email'];
回答by Alex
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
你写道你创建了变量$login = 'false'
所以'假'有一个字符串。它不是空字符串,所以 $login == true。只需将其更改为
'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;
}
回答by nazim
Declare the global in index.php.
在 index.php 中声明全局。