How to put submitted formdata in URL?

Hi,

I would like the following, but am not sure if at all possible. And IF possible: how?

Say I have a webshop and I want users to be able to search products. I made a simple form for this:

<?php
echo $this->Form->create('search', ['url' => ['controller' => 'Products','action' => 'search']]);
    echo $this->Form->control('search',['label'=>false]);
    echo $this->Form->button('submit');;
    echo $this->Form->end();

This form is in the navbar/header of every page of the website. The search term is submitted to the ‘search’ action in the ‘ProductsController’: all works fine.

But the URL where the users are redirected to is (no surprise):
https://mysite.com/products/search
(with a page with the correct results)

What I would like, is (lets presume the user searches for ‘ipad’) the URL to be:
https://mysite.com/products/search/ipad

So: The user types something (like ‘ipad’) in that searchform (anywhere on the site). And on submit, he/she is immideatly sent/redirected to
https://mysite.com/products/search/ipad

The reason I would like this, is because I want users to be able to copy a link and send that to a friend. And whan the friend opens that link, they are directly on the page with the search resluts.
To be clear: I have no issues with this last part (I have allready made a method that ‘takes’ that param from the URL and displays the correct page).
My only question/issue is: How to put that param directly in the URL on submit form.

THANKS!

You can use the FriendsOfCake/search plugin which includes a PRG component that do exactly that.

Hi Raul,
Thanks! I have seen that plugin and that would probably work. But… I would like to know how to achieve this without this plugin. Because I probably want to use this ‘trick’ (put a submitted formfield in the URL) in some other controllers (not only on search functions, but also others).
And I simply want to learn & understand how ths works.

From your answer, I understand that it IS possible? thats allready something.
Am I correct is asuming that PRG is the term that is used for this technique? Because if so, I will hopefully find some pointers when searching on discource.cakephp or Google

You can see and follow the source code of the plugin, like here (using the code for cake 3.x)

And yes, PRG is a pattern known for that use case.

In route
$routes->connect('/search/*', ['controller' => 'Products', 'action' => 'search']);

In bootstrap

$siteName =  'http://localhost/' . '/project_name/';
define('SITE_URL', $siteName);

In view page

    echo $this->Form->create('search', ['url' => [SITE_URL.'search']]);
    echo $this->Form->control('search',['label'=>false]);
    echo $this->Form->button('submit');;
    echo $this->Form->end();

Try this

Shouldn’t this be possible by simply changing the form-type to GET instead of the default POST?
Secret data of course should never be submitted using GET! :slight_smile:

See: https://book.cakephp.org/4/en/views/helpers/form.html#options-for-form-creation

Also, the controller function will need changes when using GET-form instead of POST-form.

Best regards,
D.