[英]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.