27

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.

Andreas
  • 5,305
  • 4
  • 41
  • 60

9 Answers9

62

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']);
Dency G B
  • 8,096
  • 9
  • 47
  • 78
  • Return to function is not necessary for redirect. If you use exit after redirect then it will not work. What is happening don't know – Yatin Mistry Dec 23 '15 at 13:51
  • Below there is one answer if you are using inside before action..." If you are trying to do redirect in beforeAction() you should use send() method" `return $this->redirect('/some/url',302)->send();` – Felipe Costa Oct 06 '17 at 09:11
33

If you are trying to do redirect in beforeAction() you should use send() method

 return $this->redirect('/some/url',302)->send();
Denhell
  • 526
  • 5
  • 4
11

You can redirect by this method also:

return Yii::$app->response->redirect(['user/index', 'id' => 10]);

If you want to send the Header information immediately use with send().This method adds a Location header to the current response.

return Yii::$app->response->redirect(['user/index', 'id' => 10])->send();

If you want the complete URL then use like Url::to(['user/index', 'id' => 302]) with the header of use yii\helpers\Url;.

For more information check Here. Hope this will help someone.

lalithkumar
  • 3,480
  • 4
  • 24
  • 40
8

I struggled with redirect not working for very long, none of what mentioned above was working for me, until I tried this:

Change:

return $this->redirect('site/secure');

to:

return $this->redirect(['site/secure']);

In other words, needed to enclose it within [] brackets! I am using PHP 7, might be the reason why?

Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
Perry J
  • 355
  • 3
  • 16
5

Redirects the browser to the specified URL.

This method adds a "Location" header to the current response. Note that it does not send out the header until send() is called. In a controller action you may use this method as follows:

return Yii::$app->getResponse()->redirect($url);

In other places, if you want to send out the "Location" header immediately, you should use the following code:

Yii::$app->getResponse()->redirect($url)->send();
return;
3

here is another way to do this

if(!Yii::$app->request->getIsPost()) {
    return Yii::$app->getResponse()->redirect(array('/user/index',302));
}
Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Tayyab Hussain
  • 1,658
  • 1
  • 18
  • 21
2

try by this

if(!Yii::$app->request->getIsPost()) 
{
  Yii::$app->response->redirect(array('user/index','id'=>302));
    exit(0);
}
MH2K9
  • 11,951
  • 7
  • 32
  • 49
2

Don't use exit(0); That's bad practice at the best of times. Use Yii::$app->end();

So your code would look like

$this->redirect(['index'], 302);
Yii::$app->end();

That said though the actual problem was stopping POST requests, this is the wrong solution to that problem (although it does work). To stop POST requests you need to use access control.

public function behaviors()
{
    return [
        'access' => [
            'class' => \yii\filters\AccessControl::className(),
            'only' => ['create', 'update'],
            'rules' => [
                // deny all POST requests
                [
                    'allow' => false,
                    'verbs' => ['POST']
                ],
                // allow authenticated users
                [
                    'allow' => true,
                    'roles' => ['@'],
                ],
                // everything else is denied
            ],
        ],
    ];
}
Coz
  • 1,875
  • 23
  • 21
0

if you use beforeAction then try this :

public function beforeAction($act){
  if($act->actionMethod=="actionSomeaction"){
     if(!$somecondition){
        $act->actionMethod="actionIndex"; //this will redirect to index page
     }
  }
  return parent::beforeAction($act);
}