dongshijiao6890 2010-04-04 14:12 采纳率: 0%
浏览 30
已采纳

带有友好URL的CakePHP表单提交

I have to rewrite a form in a CakePHP application. The form searches a list of car sale ads and is pretty complex (~10 fields: model, manufacturer, fuel type etc.). Currently, the form is sent via POST and is working quite well. The problem is I need to create bookmarkable URLs from the search queries and I was wondering what was the best way to accomplish this.

I tried passing 'type' => 'get' to the $form->create() method, but this generates a URL like usercars/search/?model=...&[...] and causes the search to break.

I want the URL to be something like <some routing>/<Manufacturer>/<Model>/<Fuel>/<etc>, and also to support parameter omission (i.e. <some routing>/<Manufacturer> to list all the cars from the given manufacturer, even if the other parameters are missing). By POST, currently this works. If I select only one of the params, it gives me the correct results.

I wonder if there's any way to determine cake to build the nice url from the form submission and maintain compatibility with the $this->data way of handling input in the controller. I've gone through the documentation and haven't found any way to do this.

I already have two alternatives:

  • rewriting the controller action to support function parameters instead of working with $this->data, building a route that targets the search method with the params and doing a redirect in the controller's beforeFilter() method to a URL that corresponds to the route.
  • doing javascript on the search form page to build the URL + the above way of handling it.

What would you recommend?

  • 写回答

1条回答 默认 最新

  • douliang6563 2010-04-05 20:43
    关注

    There is a built in way to do this via cake's named arguments. So if you structured your links to look like

    /controller/action/manufacturer:toyota/model:prius/

    In your controller action, they would be available in $this->passedArgs as an associative array that looked like this

    Array
    (
        [manufacturer] => toyota
        [make] => prius
    )
    

    See also:

    http://book.cakephp.org/view/46/Routes-Configuration

    Here is an example of using prototype to dynamically create the form url on submit (using prototype).

    Event.observe(window, 'dom:loaded', function(e1)
    {
        $("formid").observe('submit', function(e)
        {
            this.select(".inputclass").each(function(input)
            {
               name = input.readAttribute('name')
               val = name.match(/data\[.*\]\[(.*)\]/);
    
               if(val && val[1])
               {
                   name = val[1]
               }
    
               $("formid").action += "/" + name  + ":" + $F(input);
            });
    
            return this.submit();
        });   
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。