编辑 URL CakePHP 3 传递的格式化参数
Edit formatting parameters passed by the URL CakePHP 3
我有一个只有一个字段的表单,na input type = "text"
当提交表单时 URL 如下:
http://localhost:8765/products/search?search=notebook
我希望它按照以下形式出现:
http://localhost:8765/products/search/notebook
手动输入 URL 它完美地工作(我创建了一个能够在之后获取内容的方法
search/,还创建了一个路由,指定有一个上面的URL)。
航线代码(routes.php):
$routes->connect('/products/search/:search', ['controller' => 'Products', 'action' => 'search'],
[':search' => '\w+', 'pass' => ['search']]);
ProductsController.php代码(负责动作搜索的方法)
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));
}
}
表单代码:
<?= $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: 我想应该在这个 form
中进行更改(只是猜测)。
有两种基本解决方案
使用Js
您可以使用 js 更改 url 表单提交到:
<form
method="GET"
action="/base/url/"
onsubmit="document.location=this.action+document.getElementById('search-input-id').value; return false;"
>
<input id="search-input" />
</form>
这很简单,没有服务器端逻辑。
使用Post-Redirect-Get
或者,通过 post 提交表单,然后将用户重定向到适当的 url 以查看结果(称为 PRG 的模式)。
例如:
<form method="POST">
<input name="search-input" id="search-input" />
</form>
通过适当的控制器操作:
public function search($search = null)
{
if($this->request->is('post')) {
$term = $this->request->data['search-item'];
// Any verification/validation/logic required
// Redirect to same controller action with this search term
return $this->redirect([$term]);
}
...
// GET request, with $search term
这种技术的优点是在将用户发送到结果页面之前 control/verify/validate 搜索词是有效的 url。
我使用 jQuery
库来在提交 form
时抑制默认行为,创建一个新的 url 并进行重定向:
$("#search-form").submit(function(event){
event.preventDefault(); // suppress default behavior
action = $(this).attr('action') + '/' + document.getElementById('search').value; // create a new urldesejado
window.location.href = action; //make the redirection
});
我有一个只有一个字段的表单,na input type = "text"
当提交表单时 URL 如下:
http://localhost:8765/products/search?search=notebook
我希望它按照以下形式出现:
http://localhost:8765/products/search/notebook
手动输入 URL 它完美地工作(我创建了一个能够在之后获取内容的方法 search/,还创建了一个路由,指定有一个上面的URL)。
航线代码(routes.php):
$routes->connect('/products/search/:search', ['controller' => 'Products', 'action' => 'search'],
[':search' => '\w+', 'pass' => ['search']]);
ProductsController.php代码(负责动作搜索的方法)
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));
}
}
表单代码:
<?= $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: 我想应该在这个 form
中进行更改(只是猜测)。
有两种基本解决方案
使用Js
您可以使用 js 更改 url 表单提交到:
<form
method="GET"
action="/base/url/"
onsubmit="document.location=this.action+document.getElementById('search-input-id').value; return false;"
>
<input id="search-input" />
</form>
这很简单,没有服务器端逻辑。
使用Post-Redirect-Get
或者,通过 post 提交表单,然后将用户重定向到适当的 url 以查看结果(称为 PRG 的模式)。
例如:
<form method="POST">
<input name="search-input" id="search-input" />
</form>
通过适当的控制器操作:
public function search($search = null)
{
if($this->request->is('post')) {
$term = $this->request->data['search-item'];
// Any verification/validation/logic required
// Redirect to same controller action with this search term
return $this->redirect([$term]);
}
...
// GET request, with $search term
这种技术的优点是在将用户发送到结果页面之前 control/verify/validate 搜索词是有效的 url。
我使用 jQuery
库来在提交 form
时抑制默认行为,创建一个新的 url 并进行重定向:
$("#search-form").submit(function(event){
event.preventDefault(); // suppress default behavior
action = $(this).attr('action') + '/' + document.getElementById('search').value; // create a new urldesejado
window.location.href = action; //make the redirection
});