5

I am trying to know if a user is guest using Yii::app()->user->isGuest but I get the following notice:

Undefined property: CWebApplication::$user

Apparently the component user is not yet defined in the application, I would like to know the reason.

I had tried to check if the component is avalible via

if(isset(Yii::app()->user))

However it fails ¿How can I check if the user is a guest even when Yii::app()->user is not defined?¿Should I get this notice when using isGuest?

EDIT: I changed error_reporting directive to "E_ALL & ~E_NOTICE & ~E_DEPRECATED & ~E_STRICT" I needed to move forward. Now I got this fatal error:

Fatal error: Call to a member function getId() on a non-object in home/mario/projects/ciudad_wiki/yii/framework/web/auth/CWebUser.php on line 530

No Time Memory Function Location

  1. 0.0005 235120 {main}( ) ../index.php:0
  2. 0.0207 1548168 CApplication->run( ) ../index.php:13
  3. 0.0207 1548352 CWebApplication->processRequest( ) ../CApplication.php:162
  4. 0.0235 1725768 CWebApplication->runController( ) ../CWebApplication.php:135
  5. 0.0272 2103464 CController->run( ) ../CWebApplication.php:276
  6. 0.0279 2145872 CController->runActionWithFilters( ) ../CController.php:266
  7. 0.0279 2145984 CController->runAction( ) ../CController.php:287
  8. 0.0279 2146128 CInlineAction->runWithParams( ) ../CController.php:309
  9. 0.0279 2146688 SiteController->actionIndex( ) ../CInlineAction.php:50
  10. 0.0529 3875888 CController->render( ) ../SiteController.php:33
  11. 0.0529 3876112 CController->renderPartial( ) ../CController.php:783
  12. 0.0532 3877352 CBaseController->renderFile( ) ../CController.php:870
  13. 0.0532 3877672 CBaseController->renderInternal( ) ../CBaseController.php:96
  14. 0.0539 3918144 require( foo_dir/protected/views/site/index.php' ) ../CBaseController.php:127
  15. 0.0705 4428584 CModule->__get( ) ../CBaseController.php:199
  16. 0.0705 4428584 CModule->getComponent( ) ../CModule.php:104
  17. 0.0723 4580360 CWebUser->init( ) ../CModule.php:388
  18. 0.0736 4671144 CWebUser->getIsGuest( ) ../CWebUser.php:195
  19. 0.0736 4671240 CWebUser->getState( ) ../CWebUser.php:279
  20. 0.0736 4671320 CWebUser->getStateKeyPrefix( ) ../CWebUser.php:557

The trace is similar to the last one, Yii can't find anything in $_SESSION related to user component.

EDIT #2: I uploaded the trace with E_NOTICE on, here it is: PHP notice

EDIT #3: I updated php to 5.4 recently. When I run the application in a server with php 5.3 there is no problem, maybe there is a notice behind the scenes but not the fatal error I get with php 5.4. As bool.dev wrote: this is a complete guess.

shgnInc
  • 2,054
  • 1
  • 23
  • 34
Mario Marín
  • 131
  • 1
  • 8
  • can you check in your _config/main.php_ file if there is a `'user'` component under the `'component'` array? – bool.dev Jun 14 '12 at 02:54
  • @bool.dev yes, there is a user component: `'user'=>array( 'allowAutoLogin'=>true, 'class' => 'WebUser', ),` – Mario Marín Jun 14 '12 at 03:08
  • you can either remove the `'class'` or change it to `'class'=>'CWebUser'` , or do you have a class you made yourself named `WebUser`? – bool.dev Jun 14 '12 at 03:18
  • @bool.dev I had a class WebUser, it's gone. I removed the class but nothing has changed. – Mario Marín Jun 14 '12 at 03:24
  • this is a complete guess, can you try this: `Yii::app()->getComponent('session');// this will create session if not yet activated` and then call `Yii::app()->user;` or better try create component `Yii::app()->getComponent('user');` – bool.dev Jun 14 '12 at 04:45
  • It seems you are copying the config some other place, something missing in your configuration. – Onkar Janwa Jun 14 '12 at 05:14
  • @onkarjanwa I don't see anything special in protected/config/main when I compare it to Yii blog demo. The only difference is this [plugin](https://github.com/Nodge/yii-eauth) configuration for facebook auth. – Mario Marín Jun 14 '12 at 10:48
  • @bool.dev I added `Yii::app()->getComponent('session');
    echo '
    '; var_dump(Yii::app()->getComponent('user')); echo '
    ';` to views/layout/main.php but nothing changed.
    – Mario Marín Jun 14 '12 at 11:26
  • Try adding `user` to `preload`. – Boris Belenski Jun 14 '12 at 20:39
  • @BorisBelenski Thank you but it didn't worked. – Mario Marín Jun 14 '12 at 22:43
  • 1
    not an expert, but did you check if session component is getting created after getComponent('session') ? and what do you think is happening? any guesses? this is weird, have you asked in the yii forum? if not please ask there too. – bool.dev Jun 15 '12 at 17:12

2 Answers2

0

Sorry, I ran a massive replacent that corrupted the framework. I realized the problem was yii directory because I don't track it with git, when I set up a new production environment, in a virtual machine, everything was fine again and the only difference was the framework directory. Thanks for you help.

Mario Marín
  • 131
  • 1
  • 8
-1

I was getting a similar problem when pulling a master branch from GitLab on a virtual machine. It looked like it couldn't find the user and/or session component.

I've been around this for days, however I found that adding this in the first line of index.php of Yii (right after "

error_reporting("E_ALL & ~E_STRICT");
Ivo Pereira
  • 3,410
  • 1
  • 19
  • 24