Reputation: 5094
I am learning Yii and I wanted to ask a basic question. I have a controller and there are two actions in that controller which are as follows
class MyappController extends Controller
{
public $count=0;
public function actionInitialCount()
{
$this->count=1;
$this->redirect('secondCount');
}
public function actionSecondCount()
{
echo $this->count;
}
}
Now what i want is that when the InitialCount action has modified the $count variable to 1. the secondCount to echo it as 1 and not 0
. But it echos it as 0
only. So how can i achieve this task that if one action modifies a variable then i can get the modified values in the second action
Upvotes: 0
Views: 7490
Reputation: 752
I think, you are thinking about common variable between two life cycle of application. That is only possible with $_SESSION or other persistent variables . I hope, you understand this.
Upvotes: 1
Reputation: 567
Can you not just make count a session using Yii::app()->session['count']? What happens now is that the class is being loaded when you call actionInitialCount and again actionSecondCount so the $count will be set back to 0.
Upvotes: 1
Reputation: 2080
Now i take different examples.
It is simple PHP Program example.
class MyappController {
public $count = 0;
function InitialCount() {
$this->count = 1;
}
function SecondCount() {
print $this->count;
}
}
$ob = new MyappController();
$ob->InitialCount();
$ob->SecondCount();
Above same program but in Yii.
class MyappController extends Controller
{
public $count=0;
function __construct() {
$this->count = 7 + 8 ;
}
public function actionIndex()
{
echo $this->count ;
}
public function actionInitialCount()
{
echo $this->count=1;
}
public function actionSecondCount()
{
echo $this->count ;
}
}
We can change value by accessing it out side the class.
class MyTest
{
protected $a;
public function __construct($a)
{
$this->a = $a;
}
public function head()
{
echo $this->a;
}
public function footer()
{
echo $this->a;
}
}
$a = 7;
$obj = new MyTest($a);
echo $obj->head();
Actually when you modified your variable value, then you need to declare it, as i have declared in my 1st example, then you can get your changed value. Secondly like in ecommerce applications, when we purchase different items then it will display modified values each, so it can be accessed through session varialbes, that store information on the server. If you need your modified value in Yii and display it on the view, so you read about Yii Sessions then you can accomplish your requirement. In my example of Yii, if you redirect it to the view then it will generate error because i have made Constructor.
Hope it will help you for your understanding.
Thanks.
Upvotes: 1
Reputation: 671
Think that i have a class
class AController extends MyController{
//can be accessed here(even in the view) using $this->publicvariable
}
And I say that this is in components
class MyController extends CController{
//My public variable is declared here
}
Upvotes: 1
Reputation: 8838
In side the controller function, you have to use the count variable as
$this->count
So, complete code will be :
class MyappController extends Controller {
public $count=0;
public function actionInitialCount()
{
$this->redirect('secondCount');
}
public function actionSecondCount()
{
echo $this->count;
}
}
Upvotes: 1