我将一个表单发布到/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/