duancai7568 2016-07-15 10:37
浏览 25
已采纳

如何在控制器中保存id?

I have a action which i am calling it two time

Controller.php
UserController extends Controller {
    public actionCopy($id = false){

    if($id){
    //redireting to a view
    }
    else{
    //rediredtion to a differnet view
    }


    }
}

I am getting a id first then from first view i am again coming to Copy action with no id so else part is running nw but in this i want to get the $id which i get from first request.

I have tried to define a global variable in controller like

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
        }


        }

    }

and set it like this.

I know that if condition is not executing so $id has no value but i set $this->user first time then why it has no value.

Any help is appreaciated

  • 写回答

1条回答 默认 最新

  • douzhai1182 2016-07-15 12:01
    关注

    Just try this,

    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
                }
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)