cphalcon icon indicating copy to clipboard operation
cphalcon copied to clipboard

Session Flash doesn't work with redirect

Open MickaelViaud opened this issue 12 years ago • 4 comments

I'm using Phalcon 1.3.2 with Apache 2.4.4 and PHP 5.4.12 on Win7 x64.

I try to append in my view some flash messages but they don't display.

In services.php

$di->set('flashSess', function() {
  return new \Phalcon\Flash\Session(array(
    'error' => 'alert alert-danger',
    'success' => 'alert alert-success',
    'notice' => 'alert alert-warning'
  ));
});

In my controller

$this->flashSess->error($this->translate->getTranslation()->_("form-app-error"));
$this->response->redirect(array(
    'for' => 'dashboardIndex'
));
$this->response->send();

In my view

<?php $this->flashSess->output(); ?>

I also try in my controller

return $this->response->redirect(array(
    'for' => 'dashboardIndex'
));

But it doesn't work too.

The only things that work is

return $this->dispatcher->forward(array(
    "controller" => "application",
    "action" => "index"
));

MickaelViaud avatar Aug 11 '14 10:08 MickaelViaud

Is there any risk that when you make a http redirection the view is rendered and flash is outputed anyway? This way the flash message is being outputed but you don't see it - redirection occurs imediately.

When I make a http redirection I always switch the view off.


$this->view->disable();
$this->response->redirect(array(
    'for' => 'dashboardIndex'
));

But I may be wrong. Anyway, did it helped?

Conradaek avatar Aug 11 '14 21:08 Conradaek

Hi,

Thanks for the answer, it was a good idea but it doesn't work too :(

MickaelViaud avatar Aug 12 '14 08:08 MickaelViaud

How about:

$this->response->redirect('application/index');

mruz avatar Aug 12 '14 10:08 mruz

I found where did the problem came from. The "dashboardIndex" action use himself a redirection to an other page in some condition with the following snippet :

$this->response->redirect(array(
    'for' => 'applicationIndex'
));
$this->response->send();

When i delete the response->send(), it's work ! So don't use response->send() when using flash sessions. Thanks a lot for your answers.

MickaelViaud avatar Aug 12 '14 12:08 MickaelViaud