簡體   English   中英

Yii2重定向到登錄

[英]Yii2 redirect to login

我在控制器中的Yii2 Framework身份驗證有問題。 當我發送POST或GET時,它重定向到我不想要的登錄頁面。 我使用dektrium \\ user。 我想以訪客身份發送POST。 最后,我嘗試了所有人,但沒有任何效果。 當我使用郵遞員時,也會顯示登錄頁面。 你能說我怎么了嗎?

控制者

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;
    }
}
}

我不確定100%,因為很難僅根據您提供的代碼來判斷,但是您的行為配置不對嗎?

不應該是:

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' => ['@'],
                ]
            ],
        ],
    ]);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM