redirect - Controller 操作中的 yii2 重定向不起作用?

标签 redirect yii2

我将一个表单发布到/user/save 以保存用户数据。如果 $_SERVER['REQUEST_METHOD'] 不是“post”,我尝试重定向用户时会出现问题。

我的用户 Controller 代码

namespace app\controllers;
use Yii;
use yii\web\Controller;
use app\models\MyUser;

class UserController extends Controller {

public function actionSave() {

    if(!Yii::$app->request->getIsPost()) {
       $this->redirect('/user/index',302);
       exit(0);
   }

   //do something to save user submitted data       
}

}//~CLASS

没有办法让重定向工作。虽然 !Yii::$app->request->getIsPost() is false调用 $this->redirect does nothing!
任何帮助表示赞赏。

最佳答案

在 Yii2 中,我们需要 return()操作的结果。我认为您需要添加 return在您的重定向前面。

  return $this->redirect(['user/index']);

关于redirect - Controller 操作中的 yii2 重定向不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23893539/

相关文章:

php - CakePHP - Controller::flash() 不重定向

apache - 使用 .htaccess 重定向站点但排除一个文件夹

php - header 重定向的安全性如何?可以绕过吗?

javascript - Laravel 4-重定向到 JavaScript 代码 Blade 模板中的路由

validation - Yii2 : How to validate XSS (Cross Site Scripting) in form/model input?

php - Yii2 Assets 清除缓存

javascript - 规避asp事件处理程序

yii2 - Yii2 中无效的验证规则 : a rule must specify both attribute names and validator type,

javascript - 将数据传递到 yii2 中的下一页

javascript - 在 yii2 中选中复选框后启用字段