Yii2重定向登录

问题描述 投票:0回答:1

我在控制器中遇到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;
    }
}
}
php yii2
1个回答
0
投票

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

不应该是:

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' => 'yii\filters\AccessControl',
            'rules' => [
                [
                    'allow' => true,
                    'actions' => ['login', 'signup','last'],
                    'roles' => ['?'],
                ],
                [
                    'allow' => true,
                    'roles' => ['@'],
                ]
            ],
        ],
    ]);
}
© www.soinside.com 2019 - 2024. All rights reserved.