yii2 项目配置
程序员文章站
2022-05-08 19:22:42
...
笔记
一、yii
1.配置
'request' => [
// !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
'cookieValidationKey' => 'gR1gCUR1x2_C9hDy6wVhJqGxtw8rqXL-',
'enableCsrfValidation' => false,
'parsers' => [
'application/json' => 'yii\web\JsonParser',
// 'text/json' => 'yii\web\JsonParser',
]
],
'response' => [
'on beforeSend' => function($event) {
if(is_array($event->sender->data)){
$event->sender->format = 'json';
}
},
],
2.控制器
public $enableCsrfValidation = false;
public function beforeAction($action)
{
$id = $action->id;
if( $id == 'register' || $id == 'login' || $id == 'auto-login' ){
// code验证
if(!Auth::authCode()){
Yii::$app->getResponse()->data = ['code'=>406,'msg'=>'code无效'];
return false;
}
}
if( $id =='logout'){
// 对token验证和设备,用户id等参数进行验证
if(!Auth::authToken()){
Yii::$app->getResponse()->data = ['code'=>407,'msg'=>'签名token无效'];
return false;
}
}
if($id == 'create-role' || $id == 'user'){
// 仅进行token验证
if(!Auth::authTokenSimple()){
Yii::$app->getResponse()->data = ['code'=>408,'msg'=>'header token无效'];
return false;
}
}
return parent::beforeAction($action);
}