简体   繁体   中英

Trouble using Yii::app()->user

I have a strange problem when using Yii::app()->user , If I call it from one method I can't run almost anything until the running of the script has ended. For example let's say I have the following method:

public function actionFoo(){  
    Yii::app()->user; //just for the example, I know it doesn't mean anything
    sleep(30); //just so it won't finish right away
    echo "end of function Foo";  
} 

Now if I call the URL related to actionFoo and after I'll try to call a different URL from another tab before the first one has finished the following happened:

  1. If I enter an invalid URL that belongs to the same project I won't get a response until the end of actionFoo , meaning I won't get 404 Bad Request .
  2. If I enter a valid URL that's related to a method that also use Yii::app()->user - The same as in #1.
  3. If I enter a valid URL that's related to a method that doesn't use Yii::app()->user - everything seems to work.

Hope everything is clear and if not feel free to ask.

At the end of your action function try calling Yii::app()->end();

So something like this:

function actionFoo(){
  Yii::app()->user;

  echo 'end of function foo';
  Yii::app()->end(); // add this as the last line in your function
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM