我已经尝试了很多让用户通过我的模块登录。但总是失败。它既没有显示错误也没有登录到应用程序。 到目前为止,我已经尝试过这个。
我的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中登录其他用户实例?
答案 0 :(得分:0)
在您的模块目录中创建单独的config.php,然后添加它以将其注入您的应用程序。这是代码。 根据需要配置config.php
public function init()
{
parent::init();
\Yii::configure($this, require __DIR__ . '/config.php');
}
在你的模块课中就是这样做的。
NSLog(@"topbanner1 %lu ",sizeof(delegate.sidebannerDetailsArray))
多数人。