retaining POST parameters after login page in symfony

左心房为你撑大大i 提交于 2019-12-11 06:12:55

问题


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


回答1:


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.




回答2:


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




回答3:


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.




回答4:


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




回答5:


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_2/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.




回答6:


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.



来源:https://stackoverflow.com/questions/1543822/retaining-post-parameters-after-login-page-in-symfony

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!