2016-07-15 47 views
0

我有一个action对此我calling这两个时间如何在控制器中保存ID?

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

任何帮助appreaciated

+2

您可以尝试使用cookie或会话变量 –

+1

您可以使用所有绕过数据的正常PHP方式,如'$ _ POST,$ _GET,$ _SESSION,$ _COOKIE'等...(使用vanilla PHP或Yii语法)。使用哪一个取决于您的应用程序结构以及用户在这两个操作之间的移动方式 –

回答

0

刚刚尝试这一点,

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 
      } 
     } 
    } 
} 
相关问题