I am posting a form to /user/save in order to save user data. The problem occurs when i try to redirect the user if the $_SERVER['REQUEST_METHOD'] is NOT 'post'.
My user controller code
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
The is no way to get the redirection to work. Although !Yii::$app->request->getIsPost() is false
the call to $this->redirect does nothing!
Any help appreciated.
Please show us the controller code where you are trying to redirect. Try this: return $this->redirect(array('site/dashboard')); It will definitely work!
Yii::app()-request->redirect('/path/to/url');
In Yii2 we need to return()
the result from the action.I think you need to add a return
in front of your redirect.
return $this->redirect(['user/index']);
If you are trying to do redirect in beforeAction() you should use send() method
return $this->redirect('/some/url',302)->send();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With