我是新手Yii2开发者。我已经开始基于Basic Yii2模板创建自己的网站了。在创建注册和授权逻辑后,我遇到了一些问题。
$this->goHome()
方法重定向。默认情况下,我actionIndex
中的SiteController
,此页仍然被规则拒绝。我的数据库方案是:
$this->createTable('user', [
'id' => Schema::TYPE_PK,
'username' => Schema::TYPE_STRING . ' NOT NULL',
'email' => Schema::TYPE_STRING . ' NOT NULL',
'password_hash' => Schema::TYPE_STRING . ' NOT NULL',
'role' => Schema::TYPE_SMALLINT . ' NOT NULL',
]);
我设置了我的身份等级:
'user' => [
'identityClass' => 'app\models\User',
'enableAutoLogin' => true,
]
My User类扩展了ActiveRecord并实现了IdentityInterface。
public function getId()
{
return $this->id;
}
public static function findIdentity($id)
{
self::find()
->select('*')
->where(['id' => $id])
->one();
}
SiteController
行为方法:
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'rules' => [
[
'allow' => true,
'roles' => ['@'],
],
[
'actions' => ['login'],
'allow' => true,
'roles' => ['?']
]
],
'denyCallback' => function ($rule, $action) {
echo 'DENIED ACTION: ' . $action->actionMethod;
echo ' IS GUEST: ' . Yii::$app->user->isGuest;
echo ' SESSION IS: ' . Yii::$app->user->enableSession;
}
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'logout' => ['post'],
],
],
];
}
正如您所看到的,我在denyCallback
函数中完成了一些var_dumps。我登录后说:
IS DENIED: actionIndex IS GUEST: 1 SESSION IS: 1
还有一些日志:
登录后我有:SELECT * FROM "user" WHERE "username"='Test'
使用goHome()
重定向后:SELECT * FROM "user" WHERE "id"=1
在会话中有一个ID:__id 1
最后,如果我将客人的规则更改为:
[
'actions' => ['login', 'index'],
'allow' => true,
'roles' => ['?']
]
它显示了我需要的页面
如果我应该提供更多信息,请告诉我。提前谢谢。