yii提交数据报错yii\web\BadRequestHttpException: 您提交的数据无法被验证。 in C:\phpStudy\plugins\PHPTutorial\WWW\bljgro

在表单提交验证的时候报错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,
			]);
		}  
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

原克技术

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值