我使用以下内容分隔了后端和前端:
Backend Config / main.php
$config = [
'id' => 'app-backend',
'basePath' => dirname(__DIR__),
'controllerNamespace' => 'backend\controllers',
'bootstrap' => ['log'],
'modules' => [],
'components' => [
'request' => [
'csrfParam' => '_csrf-backend',
'cookieValidationKey' => 'sdsdsdsd-e8Fhoa1PdHzzfB2VTON9Nfh',
'class' => 'common\components\Request',
'web'=> '/backend/web',
'adminUrl' => '/cpanel'
],
'urlManager' => [
'class' => 'yii\web\UrlManager',
'enablePrettyUrl' => true,
'showScriptName' => false,
],
'user' => [
'identityClass' => 'common\models\AdminUser',
'enableAutoLogin' => true,
'identityCookie' => ['name' => '_identity-project-backend', /*'httpOnly' => true*/],
],
'session' => [
// this is the name of the session cookie used for login on the backend
'name' => 'project-backend',
'timeout' => 60*60*24*30,
],
],
'params' => $params,
];
前端Config / main.php
$config = [
'id' => 'app-frontend',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log'],
'controllerNamespace' => 'frontend\controllers',
'components' => [
'request' => [
'csrfParam' => '_csrf-backend',
'cookieValidationKey' => 'wmWhVSIv-e8Fhoa1PdHzzfB2VTON9Nfh',
'class' => 'common\components\Request',
'web' => '/frontend/web'
],
'urlManager' => [
'class' => 'yii\web\UrlManager',
'enablePrettyUrl' => true,
'showScriptName' => false,
],
'user' => [
'identityClass' => 'common\models\User',
'enableAutoLogin' => true,
'identityCookie' => ['name' => '_identity-project-frontend', /*'httpOnly' => true*/],
],
'session' => [
// this is the name of the session cookie used for login on the frontend
'name' => 'project-frontend',
'timeout' => 60*60*24*30,
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'errorHandler' => [
'errorAction' => 'site/error',
],
],
'params' => $params,
//'defaultRoute' => 'site/index'
];
现在它在普通浏览器模式下运行良好。但是,当我尝试使用隐身模式登录时,首次尝试时会出现以下错误:
无法验证您的数据提交
之后,如果我重新加载页面并尝试再次登录,它会正常工作。
我的表单是使用ActiveForm生成的,因此在登录页面中可以使用CSRF令牌。
那么如何解决这个问题呢?
答案 0 :(得分:0)
Yo可以在特定控制器/操作中指定验证错误
包括Yii类
use Yii;
在行动中
Yii::$app->controller->enableCsrfValidation = false;
或控制器内部
$this->enableCsrfValidation = false;
答案 1 :(得分:-2)
您需要设置验证错误
将此代码设置在操作
中 Yii::$app->controller->enableCsrfValidation = false;
设置完整的控制器验证错误
$this->enableCsrfValidation = false;
这将有效