我们如何在Yii 2中的模块中创建单独的用户实例?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了我们如何在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',
]);
现在我在Loginform
的app\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中登录不同的用户实例?
答案
在模块目录中创建单独的config.php,然后添加它以将其注入到您的应用程序中。这是代码。只需配置config.php即可
return [
'id' => 'api',
'basePath' => dirname(__DIR__),
//....
'components' => [
//..
'user' => [
//....
]
],
];
在你的模块类中就是这样做的。
public function init()
parent::init();
\Yii::configure($this, require __DIR__ . '/config.php');
就这样。
以上是关于我们如何在Yii 2中的模块中创建单独的用户实例?的主要内容,如果未能解决你的问题,请参考以下文章
如果他在单独的文件中创建,如何在 chrome 控制台中调用 Vue 实例