Yii errorCode释疑与汉化

  •   
  • 3774
  • PHP
  • 3
  • super_dodo
  • 2014/09/22

对于Yii的用户再登录的情况下,有时候需要知道返回来的错误码对应的提示信息。一般的UserIdentity大多这样写。

class UserIdentity extends CUserIdentity
{
 /**
  * Authenticates a user.
  * The example implementation makes sure if the username and password
  * are both 'demo'.
  * In practical applications, this should be changed to authenticate
  * against some persistent user identity storage (e.g. database).
  * @return boolean whether authentication succeeds.
  */

   private $_id;
   public function authenticate() {
     $username = strtolower($this->username);
        $user = Member::model()->find('LOWER(username)=?',array($username));
        if ($user === null){
            $this->errorCode = self::ERROR_USERNAME_INVALID;
        }
        else{
        if(!$user->validatePassword($this->password)) 
         //if ($user->password !== $user->validatePassword($this->password))
                 $this->errorCode = self::ERROR_PASSWORD_INVALID;
         else {
                 $this->_id = $user->mid;
                 $this->username=$user->username;               
                 $this->errorCode=self::ERROR_NONE;              
               }
        }
        return !$this->errorCode;
    }
    
   public function getId() {
        return $this->_id;
    }
}

UserIdentity继承自CWebUserIdentity

CWebUserIdentity又extends于CBaseUserIdentity

CBaseUserIdentity里面

const ERROR_NONE=0;
const ERROR_USERNAME_INVALID=1;
const ERROR_PASSWORD_INVALID=2;
const ERROR_UNKNOWN_IDENTITY=100;

参见 http://www.yiiframework.com/doc/api/1.1/CBaseUserIdentity/#c2217

It is not easy to meet each other in such a big world.世界这么大,能遇见,不容易。