Edit formatting of parameters passed by the CakePHP URL 3

1

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:

  

http://localhost:8765/products/search/notebook

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)

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

    
asked by anonymous 03.08.2015 / 22:39

1 answer

0

I used the%% lib as soon as the jQuery is submitted it suppresses its default behavior, create the new url and make the redirect:

$("#search-form").submit(function(event){
    event.preventDefault(); // suprimir o comportamento padrão
    action = $(this).attr('action') + '/' + document.getElementById('search').value; // criar a nova url no formato desejado
    window.location.href = action; //faz a redireção:
});
    
05.08.2015 / 18:37