Search code examples
yiiyii-extensions

yii-user extension include(WebUser.php) No such file or directory


i'm trying to install the extension of yii-user following this official tutorial

http://www.yiiframework.com/extension/yii-user/#hh2

But i'm having some problems, specially when i'm adding this

 user'=>array(
        // enable cookie-based authentication
        'class' => 'WebUser',
        'allowAutoLogin'=>true,
        'loginUrl' => array('/user/login'),

to the configuration main. When i add this code, i have this message error

include(WebUser.php) [function.include]: failed to open stream: No such file or directory

Any clue? I need to do something before?

Thanks in advance


Solution

  • I searched a little bit and i found the solution. But it wasn't in the documentation.

    So, we should create the WebUser.php in protected/components like this :

      <?php
    
    // this file must be stored in:
    // protected/components/WebUser.php
    
    class WebUser extends CWebUser {
    
    // Store model to not repeat query.
     private $UserLogin;
    
    // Return first name.
    // access it by Yii::app()->user->first_name
    function getFirst_Name(){
    $user = $this->loadUserLogin(Yii::app()->user->user_id);
    return $user->first_name;
    }  
    
    // This is a function that checks the field 'role'
    // in the User model to be equal to 1, that means it's admin
    // access it by Yii::app()->user->isAdmin()
    function isAdmin(){
    $user = $this->loadUser(Yii::app()->user->user_id);
    return intval($user->user_role_id) == 1;
    }
    
    // Load user model.
    protected function loadUserLogin($id=null)
    {
        if($this->UserLogin===null)
        {
            if($id!==null)
                $this->UserLogin=UserLogin::model()->findByPk($id);
        }
        return $this->UserLogin;
    }
    }?>
    

    and should work.