yii 如何在控制器中保存ID?

vqlkdk9b  于 2023-05-07  发布在  其他
关注(0)|答案(1)|浏览(316)

我有一个action,我调用了两次:

  1. Controller.php
  2. UserController extends Controller {
  3. public actionCopy($id = false){
  4. if($id){
  5. //redirecting to a view
  6. }
  7. else{
  8. //redirection to a different view
  9. }
  10. }
  11. }

我首先得到一个id,然后从first view我再次来到Copy action没有id,所以其他部分现在正在运行,但在这里我想得到我从第一个请求得到的$id
我尝试在控制器中定义一个全局变量,如下所示:

  1. Controller.php
  2. UserController extends Controller {
  3. public $user;
  4. public actionCopy($id= false){
  5. if($id){
  6. $this->user = $id;
  7. //redireting to a view
  8. }
  9. else{
  10. echo $this->user ;// but it has nothing
  11. //rediredtion to a differnet view
  12. }
  13. }
  14. }

像这样设置。
我知道if condition没有执行,所以$id没有值,但我第一次设置$this->user,那么为什么它没有value

zed5wv10

zed5wv101#

试试这个

  1. class UserController extends Controller {
  2. public actionCopy($id = false){
  3. if($id){
  4. //set up a session here
  5. Yii::app()->session['_data_id'] = $id;
  6. //remaining code here
  7. }
  8. else{
  9. $id=isset(Yii::app()->session['_data_id'])?Yii::app()->session['_data_id']:NULL; // the id you want from previous request
  10. if($id!=NULL){
  11. //remaining code
  12. unset(Yii::app()->session['_data_id']); //clear it after use
  13. }
  14. }
  15. }
  16. }
展开查看全部

相关问题