views:

715

answers:

6

how can i make symfony retain the POST data after login page?

for example, the cookie was expired while user was filling the form. after submitting the form user gets a redirect to a login page. and after successful login he gets a redirect back to form's "action" url without any POST data from the initial form.

is there any mechanism in symfony to handle that data, or i have to write my own?

btw, i'm using sfGuardPlugin

A: 

I have no prior knowledge about the symfony framework, but it seems normal to me that after a redirect you loose that information. Post variables are bounded to a unique HTTP request. Performing a redirect means generally doing a second HTTP request. There's no way you can store POST variables. You should use SESSION variables, instead.

Roberto Aloi
yes, it's normal, and i'm trying to find a workaround for that
kipelovets
A: 

You'll have to store it in $_SESSION. make sure you have called session_start(), then you could do something like this:

foreach($_POST as $key => $value){
   $_SESSION[$key] = $value;
}

or, just store it as a sub array in session.

$_SESSION['previousPost'] = $_POST;

I don' t have any expierience with the Sympony framework, but this will get the job done.

GSto
that is right way, but i'm looking for a symfony-style solution
kipelovets
A: 

You can give extra parameters to the redirect in symfony like so:

$this->redirect('module/action?var='.$request->getParameter('id'));
$this->redirect('module/action?var=2');

Off course, it is not very smart to send passwords like this.

A user session might be an option but try to do everything before redirecting to a new action

Kennethvr
+1  A: 

I wrote a simple filter for that. Maybe you can extend its features and you can use, here it is

class postFilter extends sfFilter
{
    public function execute($filterChain)
  {
    // Execute this filter only once
    if ($this->isFirstCall())
    {
      // reach user object
      $user    = $this->getContext()->getUser();
      // request
      $request = $this->getContext()->getRequest();
      // if user unauthenticated and if user posted a form 
      if(!$user->isAuthenticated() AND $request->isMethod('post'))
      {
        // now you can save the post parameters
        $user->setAttribute('param_name', $request->getParameter('post_data'));
        // or something like that 
      }
    }

    // Execute next filter
    $filterChain->execute();
  }
}

And add your filter to filters.yml

post_filter:
  class: postFilter

I hope it helps.

metoikos
thanks, that helped me)
kipelovets
A: 

I think the Flash attribute would be a good compromise for this. Unlike a session which would work, the values are only stored to the next page.

http://www.symfony-project.org/book/1%5F2/06-Inside-the-Controller-Layer#Flash%20Attributes

foreach($_POST as $key => $value){
    $this->getUser()->setFlash($key, $value);
}

The flash attributes will automatically be cleared after the next request.

Jestep
+1  A: 

I'm almost sure that doing a $this->forward() actually sends along all your post data.

sjobe