如何在控制器中保存ID?
how to save id in controller?
我有一个 action
我是 calling
两次
Controller.php
UserController extends Controller {
public actionCopy($id = false){
if($id){
//redireting to a view
}
else{
//rediredtion to a differnet view
}
}
}
我先得到一个 id
然后从 first view
我再次来到 Copy action
没有 id 所以其他部分是 运行 nw 但是我想要获取我从第一个请求中获取的 $id
。
我试过在控制器中定义一个全局变量,比如
Controller.php
UserController extends Controller {
public $user;
public actionCopy($id= false){
if($id){
$this->user = $id;
//redireting to a view
}
else{
echo $this->user ;// but it has nothing
//rediredtion to a differnet view
}
}
}
然后这样设置。
我知道 if condition
没有执行,所以 $id
没有价值,但我第一次设置 $this->user
那么为什么它没有 value
.
感谢任何帮助
试试这个,
class UserController extends Controller {
public actionCopy($id = false){
if($id){
//set up a session here
Yii::app()->session['_data_id'] = $id;
//remaining code here
}
else{
$id=isset(Yii::app()->session['_data_id'])?Yii::app()->session['_data_id']:NULL; // the id you want from previous request
if($id!=NULL){
//remaining code
unset(Yii::app()->session['_data_id']); //clear it after use
}
}
}
}
我有一个 action
我是 calling
两次
Controller.php
UserController extends Controller {
public actionCopy($id = false){
if($id){
//redireting to a view
}
else{
//rediredtion to a differnet view
}
}
}
我先得到一个 id
然后从 first view
我再次来到 Copy action
没有 id 所以其他部分是 运行 nw 但是我想要获取我从第一个请求中获取的 $id
。
我试过在控制器中定义一个全局变量,比如
Controller.php
UserController extends Controller {
public $user;
public actionCopy($id= false){
if($id){
$this->user = $id;
//redireting to a view
}
else{
echo $this->user ;// but it has nothing
//rediredtion to a differnet view
}
}
}
然后这样设置。
我知道 if condition
没有执行,所以 $id
没有价值,但我第一次设置 $this->user
那么为什么它没有 value
.
感谢任何帮助
试试这个,
class UserController extends Controller {
public actionCopy($id = false){
if($id){
//set up a session here
Yii::app()->session['_data_id'] = $id;
//remaining code here
}
else{
$id=isset(Yii::app()->session['_data_id'])?Yii::app()->session['_data_id']:NULL; // the id you want from previous request
if($id!=NULL){
//remaining code
unset(Yii::app()->session['_data_id']); //clear it after use
}
}
}
}