I have a single field form, a input type="text"
, when the form is submitted to URL it looks like this:
http://localhost:8765/products/search?search=notebook
I would like it to look like this when you submit the form:
typing the URL above works perfectly (I've created a method that is able to fetch the content after search / since it is in the format above, I've also created a specific route to have the URL above)
http://localhost:8765/products/search/notebook
Route code created (routes.php):
$routes->connect('/products/search/:search', ['controller' => 'Products', 'action' => 'search'],
[':search' => '\w+', 'pass' => ['search']]);
ProductsController.php code (method responsible for action search)
public function search($search)
{
if($this->request->is('get'))
{
//$product = $this->request->params['pass'];
$this->paginate = [
'fields' => ['product_name', 'quantity', 'sold', 'description', 'price', 'old_price', 'thumbnail'],
'conditions' => ['product_name LIKE' => '%'.$search.'%'],
'order' => ['price' => 'DESC'],
'limit' => 3
];
$this->set('products', $this->paginate($this->Products));
}
}
Form Code:
<?= $this->Form->create(null, ['url' => ['controller' => 'Products', 'action' => 'search'], 'type' => 'get', 'id' => 'search-form', 'class' => 'navbar-form span7 text-center']) ?>
<button class="btn btn-info" title="Favorite o Site">
<span class="glyphicon glyphicon-star"></span>
</button>
<?= $this->Form->text('search', ['class' => 'form-control', 'placeholder' => 'Search']) ?>
<?= $this->Form->button('Buscar <span class="glyphicon glyphicon-search"></span>', ['type' => 'submit', 'class' => 'btn btn-default']) ?>
<?= $this->Form->end() ?>
OBS1: I imagine that the modification should be done in this form
(It's just an assumption).
OBS2: I'm using CakePHP 3