Note that there are some explanatory texts on larger screens.

plurals
  1. POCakePHP form submission with friendly URLs
    text
    copied!<p>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 <code>POST</code> 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.</p> <p>I tried passing <code>'type' =&gt; 'get'</code> to the <code>$form-&gt;create()</code> method, but this generates a URL like <code>usercars/search/?model=...&amp;[...]</code> and causes the search to break. </p> <p>I want the URL to be something like <code>&lt;some routing&gt;/&lt;Manufacturer&gt;/&lt;Model&gt;/&lt;Fuel&gt;/&lt;etc&gt;</code>, and also to support parameter omission (i.e. <code>&lt;some routing&gt;/&lt;Manufacturer&gt;</code> to list all the cars from the given manufacturer, even if the other parameters are missing). By <code>POST</code>, currently this works. If I select only one of the params, it gives me the correct results.</p> <p>I wonder if there's any way to determine cake to build the nice url from the form submission and maintain compatibility with the <code>$this-&gt;data</code> way of handling input in the controller. I've gone through the documentation and haven't found any way to do this.</p> <p>I already have two alternatives:</p> <ul> <li>rewriting the controller action to support function parameters instead of working with <code>$this-&gt;data</code>, building a route that targets the search method with the params and doing a redirect in the controller's <code>beforeFilter()</code> method to a URL that corresponds to the route.</li> <li>doing javascript on the search form page to build the URL + the above way of handling it.</li> </ul> <p>What would you recommend?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload