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条)

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)