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:
echo $this->Form->create('search', ['url' => ['controller' => 'Products','action' => 'search']]);
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):
(with a page with the correct results)
What I would like, is (lets presume the user searches for ‘ipad’) the URL to be:
So: The user types something (like ‘ipad’) in that searchform (anywhere on the site). And on submit, he/she is immideatly sent/redirected to
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.
You can use the FriendsOfCake/search plugin which includes a PRG component that do exactly that.
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.
$routes->connect('/search/*', ['controller' => 'Products', 'action' => 'search']);
$siteName = 'http://localhost/' . '/project_name/';
In view page
echo $this->Form->create('search', ['url' => [SITE_URL.'search']]);
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!
Also, the controller function will need changes when using GET-form instead of POST-form.