I need to set a flash and then get the Response object of another action and return it. So I made a simple test - created whole new Yii2 project and added following to site/index action:
Yii::$app->getSession()->setFlash('error', 'bad error');
return Yii::$app->getResponse()->redirect('site/about');
Flash not visible, but when I changed to:
Yii::$app->getSession()->setFlash('error', 'bad error');
return $this->redirect('site/about');
flash message is visible.
How can I return Response object of another action and still keep the flash on?
Unable to reproduce this issue. With plain basic application it is working in both cases.
Same here.
I was using advanced application but I'm not sure how it could be relevant. Maybe the issue is caused by WAMP server... I'll post solution if I find it.
I have same issue too. I occur in the following cases:
namespace app\helpers;
use Yii;
use yii\helpers\Url;
trait Web
{
public function save($model, $message)
{
if ($model->load(Yii::$app->request->post()) && $model->save()) {
Yii::$app->session->setFlash('success', $message);
return Yii::$app->response->redirect(Url::to(['view', 'id' => $model->id]));
}
}
}
Controller:
namespace app\controllers;
use yii\web\Controller;
use app\helpers\Web;
class PostController extends Controller
{
use Web;
public function actionCreate()
{
$post = new Post;
$this->save($post, 'success!');
return $this->render('create', compact('post'));
}
}
In layout view I use the Alert widget .
Same issue. Flash works in my localhost, but does not work in my linux server.
Edit: It seems to be an issue with redirect. If your initial redirect gets redirected again, the message is lost. I saw a 302 redirect in the URL that had this issue. This somehow got fixed in my case.
Same issue, It works for me if in the controller I add a return in the same line to call the trait method, but it isn't a good solution for me because I only need return case of error.
Any other solution?
I have a solution
if(Yii::$app->getResponse()->getStatusCode() != 302) {
Yii::$app->session->getFlash('error');
}
Guys this one got solved for me many days ago lol. DO NOT call $this->redirect($path) with out a return.
Correct code is:
Yii::$app->session->setFlash('success', Some Message.');
return $this->redirect(['path']);
Yii::$app->getSession()->setFlash('error', 'bad error');
Yii::$app->getResponse()->redirect(['site/about']);
Yii::$app->getResponse()->send();
exit;
I also have similar issue, and just realized that I can not use another function to redirect.
NOT working:
public function actionApply()
{
$items = $post['items'];
$this->checkRequirements($items);
}
private function checkRequirements($items)
{
if (count($items) > static::MAX_ITEM)
{
Yii::$app->session->setFlash('error', 'Number of items can not exceed ' . static::MAX_ITEM);
return $this->redirect('/reimburse/apply/');
}
}
Works :
public function actionApply()
{
$items = $post['items'];
if (count($items) > static::MAX_ITEM)
{
Yii::$app->session->setFlash('error', 'Number of items can not exceed ' . static::MAX_ITEM);
return $this->redirect('/reimburse/apply/');
}
// more action....
}
I have the same issue, and only the solution of @Mirocow worked for me without the "return" keyword.
Is that the correct way?
Most helpful comment
I have a solution