2016-07-15 43 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 
      } 
     } 
    } 
} 
相關問題