2017-08-06 23 views
2

我已經嘗試了很多,通過我的模塊進行用戶登錄。但總是失敗。它既沒有顯示錯誤,也沒有登錄到應用程序。 到目前爲止,我已經嘗試過。我們如何在Yii 2的模塊中創建單獨的用戶實例?

module/moduleName/moduleName.php初始化函數是

public function init() 
{ 
    parent::init(); 

    // custom initialization code goes here 

    Yii::$app->set('user', [ 
     'class' => 'yii\web\User', 
     'identityClass' => 'app\module\modulesName\models\UserTable', 
     'enableAutoLogin' => false, 
     'loginUrl' => ['moduleName/default/login'], 
     'identityCookie' => ['name' => 'module', 'httpOnly' => true], 
     'idParam' => 'id', //this is important ! 
    ]); 

    Yii::$app->set('session', [ 
     'class' => 'yii\web\Session', 
     'name' => '_adminSessionId', 
    ]); 
} 

現在我Loginformapp\module\moduleName\models\LoginForm

<?php 

namespace app\modules\moduleName\models; 

use Yii; 
use yii\base\Model; 

/** 
* LoginForm is the model behind the login form. 
* 
* @property User|null $user This property is read-only. 
* 
*/ 
class LoginForm extends Model 
{ 
    public $username; 
    public $password; 
    public $rememberMe = true; 

    private $_user = false; 


    /** 
    * @return array the validation rules. 
    */ 
    public function rules() 
    { 
     return [ 
      // username and password are both required 
      [['username', 'password'], 'required'], 
      // rememberMe must be a boolean value 
      ['rememberMe', 'boolean'], 
      // password is validated by validatePassword() 
      ['password', 'validatePassword'], 
     ]; 
    } 

    /** 
    * Validates the password. 
    * This method serves as the inline validation for password. 
    * 
    * @param string $attribute the attribute currently being validated 
    * @param array $params the additional name-value pairs given in the rule 
    */ 
    public function validatePassword($attribute, $params) 
    { 
     if (!$this->hasErrors()) { 
      $user = $this->getUser(); 

      if (!$user || !$user->validatePassword($this->password)) { 
       $this->addError($attribute, 'Incorrect username or password.'); 
      } 
     } 
    } 

    /** 
    * Logs in a user using the provided username and password. 
    * @return bool whether the user is logged in successfully 
    */ 
    public function login() 
    { 
     if ($this->validate()) { 
      return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600*24*30 : 0); 
     } 
     return false; 
    } 

    /** 
    * Finds user by [[username]] 
    * 
    * @return User|null 
    */ 
    public function getUser() 
    { 
     if ($this->_user === false) { 
      $this->_user = UserTable::findByUsername($this->username); 
     } 

     return $this->_user; 
    } 
} 

終於在app\module\moduleName\models\UserTable我的登錄模式是

<?php 

namespace app\modules\moduleName\models; 

use Yii; 

/** 
* @property integer $id 
* @property string $fullName 
* @property string $email 
* @property string $password 
* @property string $authkey 
* @property string $accessToken 
* @property integer $authStatus 

*/ 
class UserTable extends \yii\db\ActiveRecord implements \yii\web\IdentityInterface 
{ 
    /** 
    * @inheritdoc 
    */ 
    public static function tableName() 
    { 
     return 'usertable'; 
    } 

    /** 
    * @inheritdoc 
    */ 
    public function rules() 
    { 
     return [ 
      [['fullName', 'email', 'password'], 'required'], 
      [['authStatus'], 'integer'], 
      [['fullName', 'email', 'password'], 'string', 'max' => 50], 
      [['authkey', 'accessToken'], 'string', 'max' => 75], 
      // [['fullName', 'password'], 'unique', 'targetAttribute' => ['fullName', 'password'], 'message' => 'The combination of Full Name and Password has already been taken.'], 
     ]; 
    } 

    /** 
    * @inheritdoc 
    */ 
    public function attributeLabels() 
    { 
     return [ 
      'id' => 'ID', 
      'fullName' => 'Full Name', 
      'email' => 'Email', 
      'password' => 'Password', 
      'authkey' => 'Authkey', 
      'accessToken' => 'Access Token', 
      'authStatus' => 'Auth Status', 
     ]; 
    } 

    /** 
    * @return \yii\db\ActiveQuery 
    */ 

    // User login Codes 

    public function getId() 
    { 
     return $this->id; 
    } 

    /** 
    * @inheritdoc 
    */ 
    public function getAuthKey() 
    { 
     return $this->authKey; 
    } 



    /** 
    * @inheritdoc 
    */ 
    public function validateAuthKey($authKey) 
    { 
     return $this->authKey === $authKey; 
    } 

    /** 
    * Validates password 
    * 
    * @param string $password password to validate 
    * @return bool if password provided is valid for current user 
    */ 
    public function validatePassword($password) 
    { 

    return $this->password ===($password); 
    } 

    public static function findIdentity($id) 
    { 
     return static::findOne($id); 
    } 
    public static function findIdentityByAccessToken($token,$type=null) 
    { 
     return $this->accessToken; 
    } 
    public static function findbyUsername($uname) 
    { 

     return self::find()->where(['email' => $uname])->one(); 
    } 
} 

在結束我的defaultcontroller在模塊是

public function actionIndex() 
{ 
    if (!Yii::$app->user->isGuest){ 
     var_dump('a'); 
     exit(); 
     return $this->render('index'); 
    } else { 
     return $this->redirect(['login']); 
    } 
} 

public function actionLogin() 
{ 
    if (!Yii::$app->user->isGuest) { 
     return $this->goHome(); 
    } 

    $model = new LoginForm(); 
    if ($model->load(Yii::$app->request->post())) { 
     return $this->redirect(['index']); 
    } 
    return $this->render('login', [ 
     'model' => $model, 
    ]); 
} 

我不知道如何應用程序沒有登錄。我試過3天了,我沒有得到這個問題的任何解決方案。它既不顯示我的錯誤,也沒有登錄?我如何在yii2中登錄不同的用戶實例?

+0

不要在數據庫模型中保留規則。如果您創建驗證然後保存的獨立模型會更好。 –

回答

0

在你的模塊目錄下創建單獨的config.php,然後將它添加到你的應用程序中。這是代碼。 只要你想

return [ 
    'id' => 'api', 
    'basePath' => dirname(__DIR__), 
    //.... 
    'components' => [ 
     //.. 
     'user' => [ 
      //.... 
     ] 
    ], 
]; 

在你的模塊類只是confugure config.php文件只是做這樣的。

public function init() 
{ 
    parent::init(); 

    \Yii::configure($this, require __DIR__ . '/config.php'); 
} 

那就是全部。

相關問題