我是Yii2中的新手,现在我手动构建新的应用程序(使用Mark Safronov书)。 我试图将我的控制器重定向到某个地方而且它没有工作。
像这样:
return $this->redirect(array('/site/dashboard'));
我认为这取决于我的设置,你有什么想法吗?我使用的是niceurls,这是我的httaccess文件:
RewriteEngine on
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php
这是我的观看代码
我有下一个配置:common / config / bootstrap.php
Yii::setAlias('yii2_book', dirname(dirname(__DIR__)) . '/yii2_book');
我的控制器中的代码:
public function actionAdd()
{
$customer = new CustomerRecord();
$phone = new PhoneRecord();
if($customer->load(Yii::$app->request->post()) && $customer->validate() && $phone->load(Yii::$app->request->post())){
$this->store($this->makeCustomer($customer, $phone));
return $this->redirect(['index']);
}
return $this->render('add',compact('customer','phone'));

堆栈跟踪:
2018-01-05 11:30:03 [::1][-][-][error][yii\web\HttpException:400] yii\web\BadRequestHttpException: Unable to verify your data submission. in /Users/sergejandrejkin/PhpstormProjects/yii2_book/vendor/yiisoft/yii2/web/Controller.php:166
Stack trace:
#0 /Users/sergejandrejkin/PhpstormProjects/yii2_book/vendor/yiisoft/yii2/base/Controller.php(155): yii\web\Controller->beforeAction(Object(yii\base\InlineAction))
#1 /Users/sergejandrejkin/PhpstormProjects/yii2_book/vendor/yiisoft/yii2/base/Module.php(528): yii\base\Controller->runAction('add', Array)
#2 /Users/sergejandrejkin/PhpstormProjects/yii2_book/vendor/yiisoft/yii2/web/Application.php(103): yii\base\Module->runAction('customers/add', Array)
#3 /Users/sergejandrejkin/PhpstormProjects/yii2_book/vendor/yiisoft/yii2/base/Application.php(386): yii\web\Application->handleRequest(Object(yii\web\Request))
#4 /Users/sergejandrejkin/PhpstormProjects/yii2_book/web/index.php(12): yii\base\Application->run()
#5 {main}
2018-01-05 11:30:03 [::1][-][-][info][application] $_POST = [
'_csrf' => 'oUDxUGHONyj8ER3vQc9cvdpMsCJh6DXlPzsnxRCxfu3AArIlA7lYR8tnbN8UuDf_937iFlKnc6tGVhCNQvQKiA=='
'CustomerRecord' => [
'name' => ''
'birth_date' => ''
'notes' => ''
]
'PhoneRecord' => [
'number' => ''
]
]
$_SERVER = [
'REDIRECT_STATUS' => '200'
'HTTP_HOST' => 'localhost:8888'
'HTTP_CONNECTION' => 'keep-alive'
'CONTENT_LENGTH' => '208'
'HTTP_CACHE_CONTROL' => 'max-age=0'
'HTTP_ORIGIN' => 'http://localhost:8888'
'HTTP_UPGRADE_INSECURE_REQUESTS' => '1'
'CONTENT_TYPE' => 'application/x-www-form-urlencoded'
'HTTP_USER_AGENT' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36'
'HTTP_ACCEPT' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8'
'HTTP_REFERER' => 'http://localhost:8888/web/customers/add'
'HTTP_ACCEPT_ENCODING' => 'gzip, deflate, br'
'HTTP_ACCEPT_LANGUAGE' => 'ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7'
'PATH' => '/usr/bin:/bin:/usr/sbin:/sbin'
'SERVER_SIGNATURE' => ''
'SERVER_SOFTWARE' => 'Apache/2.2.31 (Unix) mod_wsgi/3.5 Python/2.7.13 PHP/7.0.15 mod_ssl/2.2.31 OpenSSL/1.0.2j DAV/2 mod_fastcgi/2.4.6 mod_perl/2.0.9 Perl/v5.24.0'
'SERVER_NAME' => 'localhost'
'SERVER_ADDR' => '::1'
'SERVER_PORT' => '8888'
'REMOTE_ADDR' => '::1'
'DOCUMENT_ROOT' => '/Users/sergejandrejkin/PhpstormProjects/yii2_book'
'SERVER_ADMIN' => 'you@example.com'
'SCRIPT_FILENAME' => '/Users/sergejandrejkin/PhpstormProjects/yii2_book/web/index.php'
'REMOTE_PORT' => '49253'
'REDIRECT_URL' => '/web/customers/add'
'GATEWAY_INTERFACE' => 'CGI/1.1'
'SERVER_PROTOCOL' => 'HTTP/1.1'
'REQUEST_METHOD' => 'POST'
'QUERY_STRING' => ''
'REQUEST_URI' => '/web/customers/add'
'SCRIPT_NAME' => '/web/index.php'
'PHP_SELF' => '/web/index.php'
'REQUEST_TIME_FLOAT' => 1515148203.45
'REQUEST_TIME' => 1515148203
'argv' => []
'argc' => 0
]

答案 0 :(得分:1)
例外说:
yii\web\BadRequestHttpException: Unable to verify your data submission.
这告诉你两件事:
答案 1 :(得分:0)
如果只是通过ob_end_clean();
清除它
如果在重定向之前存在任何回声或标题,那么它将不起作用!
尝试:
flush();
或尝试
ob_start()
如果没有人可以尝试使用javascript方法:
echo "<script type='text/javascript'> window.location='http://your complete url .. '; </script>";
这将100%有效,祝你好运。
答案 2 :(得分:0)
我遇到了同样的问题。考虑store(…)
方法正常,请尝试在重定向后添加Yii::$app->end()
。所以
return $this->redirect(['index']);
变为
$this->redirect(['index']);
Yii::$app->end();
答案 3 :(得分:-1)
是的,请检查您的配置设置。
'urlManager' => [
'enablePrettyUrl' => true,
'enableStrictParsing' => true,
'showScriptName' => false,
'rules' => [
],
还应该启用mod_rewrite模块。 希望它有所帮助!