我想为SEO进行URL编辑。数据将从表格输入。根据输入的过滤器;
简单网址:http://127.0.0.1:8000/query
列出过滤数据后,URL:http://127.0.0.1:8000/query/“输入文本值” /“城市名称” /“类别名称”
我希望它发生。我该怎么办?
注意:数据来自缓存。
控制器索引:
public function index()
{
return view('front.home.index');
}
刀片式房屋索引表格:
<form method="get" action="{{ route('query') }}">
@Csrf
<div class="row align-items-center">
<div class="col-lg-12 col-xl-4 no-sm-border border-right">
<input type="text" name="what" class="form-control">
</div>
<div class="col-lg-12 col-xl-3 no-sm-border border-right">
<div class="wrap-icon">
<span class="icon icon-room"></span>
<select class="form-control" name="city_id" id="">
<option value="0">All</option>
@foreach(json_decode($cities) as $city)
<option value="{{ $city->id }}">{{ $city->name }}</option>
@endforeach
</select>
</div>
</div>
<div class="col-lg-12 col-xl-3">
<div class="select-wrap">
<span class="icon"><span class="icon-keyboard_arrow_down"></span></span>
<select class="form-control" name="category_id" id="">
<option value="0">All</option>
@foreach(json_decode($categories) as $category)
<option value="{{ $category->id }}">{{ $category->name }}</option>
@endforeach
</select>
</div>
</div>
<div class="col-lg-12 col-xl-2 ml-auto text-right">
<input type="submit" class="btn btn-primary" value="Search">
</div>
</div>
</form>
web.php
Route::get('/query', 'HomeController@query')->name('query');
答案 0 :(得分:1)
您可以这样定义路线:
Route::get('/query/{value?}/{city?}/{name?}', 'HomeController@query')->name('query');
这样,在您的控制器中:
public function query($value = null, $city = null, $name = null)
{
if ($value && $city && $name)
{
// return the results of your filtered query
}
// return the default query without filters (?)
}
然后在您看来:
<form method="GET" action="route('query', ['value' => $value, 'city' => $city, 'name' => $name])">
// ...
查看文档的this section。
答案 1 :(得分:0)
通过GET
方法提交的表单使用查询参数(?var=value&var2=value2
)而不是url参数,因此这里有两种方法:
第一种方法,在您的视图中添加以下内容:
// Adjust the selector if there are more forms in your page
let form = document.querySelector('form')
form.addEventListener('submit', function(e) {
// Prevent normal submissions
e.preventDefault();
// Get form action and values
let a = form.getAttribute('action');
let q = form.querySelector('input[name="what"]').value;
// Here I'm using the text instead of the values or there's no point prettifying the url
let cat = form.querySelector('select[name="category_id"] option:checked').innerText;
let c = form.querySelector('select[name="city_id"] option:checked').innerText;
// Join the parts together
let dest = [a, q, c, cat].join('/');
// Redirect
window.location = dest;
});
第二种方法,检查查询字符串是否包含任何指示表单提交的参数,然后重定向。您可以将其拆分为仅处理以下形式的其他控制器方法:
public function query($what = null, $city = null, $category = null, Request $request)
{
$queryParams = $request->query();
if (isset($queryParams['what'])) {
$q = $queryParams['what'];
$c = $queryParams['city_id'];
$cat = $queryParams['category_id'];
// TODO: Retrieve city and category values from database
return redirect('query', ['what' => $q, 'city' => $city, 'category' => $category]);
}
}