Yii2重定向登录

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Yii2重定向登录相关的知识,希望对你有一定的参考价值。

我在控制器中遇到Yii2 Framework身份验证问题。当我发送POST或GET时,它会重定向到登录页面,这是我不想要的。我使用dektrium user。我想以客人身份发送POST。最后我试过每个人都没有用。当我使用Postman时,也会显示登录页面。你能说我有什么不对吗?

调节器

class DhtController extends Controller implements ISensorController
{
public $enableCsrfValidation =false;

public function behaviors()
{
    return ArrayHelper::merge(parent::behaviors(), [
        'authenticator' => [
            'class' => CompositeAuth::className(),
            'authMethods' => [
                ['class' => HttpBearerAuth::className()],
                ['class' => QueryParamAuth::className(), 'tokenParam' => 'accessToken'],
            ]
        ],
        'exceptionFilter' => [
            'class' => ErrorToExceptionFilter::className()
        ],
        [
            'allow' => true,
            'actions' => ['login', 'signup','last'],
            'roles' => ['?'],
        ],
    ]);
}

public function actionIndex()
{
    $query = DhtData::find();

    $pagination = new Pagination(['defaultPageSize' => 15,
        'totalCount' => $query->count(),
    ]);
    $dhts = $query->orderBy('id')
        ->offset($pagination->offset)
        ->limit($pagination->limit)
        ->all();

    return $this->render('index', [
        'dhts' => $dhts,
        'pagination' => $pagination
    ]);

}
   public function actionLast()
{
    if (Yii::$app->request->isGet) {
        $max = DhtData::find()->max('id');
        $dht = DhtData::findOne($max);

        return $this->render('last', [
            'temp' => $dht->Temperature,
            'hum' => $dht->Humidity,
            'date' => $dht->Created_at
        ]);
    }

    if (Yii::$app->request->isPost) {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $data = Json::decode(Yii::$app->request->getRawBody());

        if ($data) {
            $filter = new DhtSearch($data);
            $records = DhtData::find();
            if ($filter->beginDate)
                $records = $records->andWhere(['>=', 'Created_at', $filter->beginDate]);

            if ($filter->endDate)
                $records = $records->andWhere(['<=', 'Created_at', $filter->endDate]);

            $records = $records->asArray()->orderBy('Created_at DESC')->all();
            $max = $records[0];
            $json = JSON::encode($max);
        } else {
            $max = DhtData::find()->max('id');
            $dht = DhtData::findOne($max);
            $json = JSON::encode($dht);
        }
        Yii::$app->response->content = $json;
    }
}
}
答案

我不是百分之百确定,因为很难根据你给出的代码判断但是你的行为配置不对吗?

不应该是:

public function behaviors()
{
    return ArrayHelper::merge(parent::behaviors(), [
        'authenticator' => [
            'class' => CompositeAuth::className(),
            'authMethods' => [
                ['class' => HttpBearerAuth::className()],
                ['class' => QueryParamAuth::className(), 'tokenParam' => 'accessToken'],
            ]
        ],
        'exceptionFilter' => [
            'class' => ErrorToExceptionFilter::className()
        ],
        'access' => [
            'class' => 'yiifiltersAccessControl',
            'rules' => [
                [
                    'allow' => true,
                    'actions' => ['login', 'signup','last'],
                    'roles' => ['?'],
                ],
                [
                    'allow' => true,
                    'roles' => ['@'],
                ]
            ],
        ],
    ]);
}

以上是关于Yii2重定向登录的主要内容,如果未能解决你的问题,请参考以下文章

在 YII2 中使用自动注销,需要在再次登录后将用户重定向到用户在自动注销之前的相同 URL

302 登录重定向后被 IE 删除的 URL 片段

Yii2 使用计时器重定向

yii2 beforeAction 重定向问题

在 PHP 重定向期间处理片段标识符

Yii2 重定向到上一页