开发者

Symfony/Routing: Using POST to Avoid Query Params in URL

开发者 https://www.devze.com 2023-02-22 03:22 出处:网络
I want to pass the id from one action to the next action, but I do not want it seen in the URL. Is there a way to hide it?

I want to pass the id from one action to the next action, but I do not want it seen in the URL. Is there a way to hide it?

Using Symfony, I have created a sign-up done page whose URL should be /signup/done When the form is processed, I want to redirect to the signupSuccess action and pass the recently created ID for future use. So I use...

$this->redirect('@signup_done?id=' . $sign_up->getId());

The routing is as follows:

signup_done:
  url:   /signup/done
  param: { module: SignUp, action: signupDone }

I have avoided the :id at the end because I don't want it in the URL. But the resulting URL is /signup/done?id=1

Just as 开发者_如何学运维an experiment, I tried putting this on a template.

<?php echo link_to('Sign-up again', '@signup_done?id=1', 'post=true') ?>

Even when using post, the query parameter appears in the URL.

The need is: I want to pass the id from one action to the next action, but I do not want it seen in the URL. Is there a way to hide it?


I set the id as a parameter in the request using $request->setParameter('id', $id) and it was available in the next action.

This kept the URL clean.


If you want to post, you need a form. Back in symfony 1.2 there were helpers that you could call and made you just that - but they were removed for the promotion of promoting good code.

Depending on how you want the 'Sign up again' text to look, you can either create a simple form and a submit button, or create a link, attach a click listener, and create a form there via JS, finally post it.

Any parameter that you pass to the route in url_for, link_to and such end up in the get parameters.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号