doufu7835 2013-11-29 13:15
浏览 61
已采纳

Symfony 2:保持表单数据跨重定向

In a Symfony 2 application, I am trying to implement a form with a search box and some additional filters (choice widget). The search box looks for a record in the DB and then displays details about that record. The filters are used to display or hide certain types of details.

This works if no or one record is found because I can then stay within the same controller and action. However, if more than one record is found, I render a different template which displays a selection dialog. Each record in the list is a link back to the original page, but with the record id as a parameter.

When I click on that link, I loose the form data. What is the best way to retain form data across a redirect here (or conditionally display a selection dialog without a redirect)?

  • 写回答

2条回答 默认 最新

  • douliwang6896 2013-11-29 14:03
    关注

    There are 2 approaches. One is simple and easy to implement, second allows robots to crawl your search results but needs more time spending and requires external storage.

    Using Session object

    Symfony's Session object has so-called flashbag where you can store random data. Basicly it's the same session parameter but clearly indicates that it can be removed in any moment.

    Updated @Darragh pointed out that flashbag can be retrieved only once. So I recommend you using Session object instead. It's stored as session service in container or app.session in twig. Session is started automatically first time you write something there.

    How it can be implemented:

    1. user submits form
    2. you perform actual search and in the same time save search parameters to session

    Upon going back to search results you check this session and retrieve your search set from there.

    Using storage

    Main idea is each unique set of search parameters represents own search page with results e.g. /search?hash=ASIDJA12SLDS5KSK2.

    What's this and how it works? Pretty simple: you store each unique set of search parameters in db along with unique hash for this set of parameters. Hash function is not required to be reversible but it should be constant: each set of search parameters should have only one constant hash. Otherwise you'll have 2 different pages with same results that crawling bots can identify as duplicate content.

    Logic is following:

    1. user submits search form
    2. you hash search parameters and redirect user to /search?hash=*hash*
    3. you retrieve search parameters based on hash from storage and perform actual search

    If user navigates back to search results he will land to /search?hash=*hash* again. If you implement steps 2 and 3 correctly you don't even need to handle this case.

    Optionally you can use reversible hash algorithm (e.g. base64). In this case you don't need external storage but be sure that you won't end up with too big url

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能