在表单提交验证的时候报错yii\web\BadRequestHttpException: 您提交的数据无法被验证。 in C:\phpStudy\plugins\PHPTutorial\WWW\bljgroups\vendor\yiisoft\yii2\web\Controller.php:166
具体的错误信息是:
An Error occurred while handling another error:
yii\base\InvalidRouteException: Unable to resolve the request “site/error”. in C:\phpStudy\plugins\PHPTutorial\WWW\bljgroups\vendor\yiisoft\yii2\base\Module.php:532
Stack trace:
#0 C:\phpStudy\plugins\PHPTutorial\WWW\bljgroups\vendor\yiisoft\yii2\web\ErrorHandler.php(97): yii\base\Module->runAction(‘site/error’)
#1 C:\phpStudy\plugins\PHPTutorial\WWW\bljgroups\vendor\yiisoft\yii2\base\ErrorHandler.php(111): yii\web\ErrorHandler->renderException(Object(yii\web\BadRequestHttpException))
#2 [internal function]: yii\base\ErrorHandler->handleException(Object(yii\web\BadRequestHttpException))
#3 {main}
Previous exception:
yii\web\BadRequestHttpException: 您提交的数据无法被验证。 in C:\phpStudy\plugins\PHPTutorial\WWW\bljgroups\vendor\yiisoft\yii2\web\Controller.php:166
Stack trace:
#0 C:\phpStudy\plugins\PHPTutorial\WWW\bljgroups\vendor\yiisoft\yii2\base\Controller.php(154): yii\web\Controller->beforeAction(Object(yii\base\InlineAction))
#1 C:\phpStudy\plugins\PHPTutorial\WWW\bljgroups\vendor\yiisoft\yii2\base\Module.php(523): yii\base\Controller->runAction(‘login’, Array)
#2 C:\phpStudy\plugins\PHPTutorial\WWW\bljgroups\vendor\yiisoft\yii2\web\Application.php(102): yii\base\Module->runAction(‘download/login’, Array)
#3 C:\phpStudy\plugins\PHPTutorial\WWW\bljgroups\vendor\yiisoft\yii2\base\Application.php(380): yii\web\Application->handleRequest(Object(yii\web\Request))
#4 C:\phpStudy\plugins\PHPTutorial\WWW\bljgroups\www\mobile.php(20): yii\base\Application->run()
#5 {main}
这个问题貌似Yii1.x跟Yii2都会出现,本人是使用Yii2.0出现的问题。
该问题由于Yii csrf引起,只要局部关闭就可以
添加到控制器
public $enableCsrfValidation = false;
class DownloadController extends Controller{
public $enableCsrfValidation = false;
public function actions()
{
return [
'error' => [
'class' => 'yii\web\ErrorAction',
],
];
}
public function actionIndex()
{
if (Yii::$app->session['usersdown']) {
// return $this->redirect(Url::toRoute(['/about/down']));
$data_a = FileDown::find()->where(['status'=>1,'father'=>0])->orderBy('createtime DESC')->all();
$data_b = FileDown::find()->where(['status'=>1,'father'=>1])->orderBy('createtime DESC')->all();
$data_c = FileDown::find()->where(['status'=>1,'father'=>2])->orderBy('createtime DESC')->all();
$data_d = FileDown::find()->where(['status'=>1,'father'=>3])->orderBy('createtime DESC')->all();
return $this->render('index',[
'model_a' => $data_a,
'model_b' => $data_b,
'model_c' => $data_c,
'model_d' => $data_d,
]);
}
$model=new UserFile();
return $this->redirect(Url::toRoute(['/download/login']));
// return $this->render('login', [
// 'model' => $model,
// ]);
}
public function actionLogin(){
$model=new UserFile();
if (Yii::$app->session['usersdown']) {
// return $this->redirect(Url::toRoute(['/about/down']));
$data_a = FileDown::find()->where(['status'=>1,'father'=>0])->orderBy('createtime DESC')->all();
$data_b = FileDown::find()->where(['status'=>1,'father'=>1])->orderBy('createtime DESC')->all();
$data_c = FileDown::find()->where(['status'=>1,'father'=>2])->orderBy('createtime DESC')->all();
$data_d = FileDown::find()->where(['status'=>1,'father'=>3])->orderBy('createtime DESC')->all();
return $this->render('index',[
'model_a' => $data_a,
'model_b' => $data_b,
'model_c' => $data_c,
'model_d' => $data_d,
]);
}
// $model=new UserFile();
if ($model->load(Yii::$app->request->post()) && $model->login()) {
Yii::$app->session['usersdown']=$model->username;
Log::log(['level'=>'info', 'category'=>'sys', 'sys'=>$model->username.'登录成功', 'msg'=>'登录IP:'.$_SERVER['REMOTE_ADDR']]);
$data_a = FileDown::find()->where(['status'=>1,'father'=>0])->orderBy('createtime DESC')->all();
$data_b = FileDown::find()->where(['status'=>1,'father'=>1])->orderBy('createtime DESC')->all();
$data_c = FileDown::find()->where(['status'=>1,'father'=>2])->orderBy('createtime DESC')->all();
$data_d = FileDown::find()->where(['status'=>1,'father'=>3])->orderBy('createtime DESC')->all();
return $this->render('index',[
'model_a' => $data_a,
'model_b' => $data_b,
'model_c' => $data_c,
'model_d' => $data_d,
]);
} else {
return $this->render('login', [
'model' => $model,
]);
}
}