php – Laravel搜索功能在页面刷新后崩溃
作者:互联网
我遵循简单的搜索功能
public function search(Request $request)
{
$posts = Post::where('title', 'like', '%'.$request->term.'%')->paginate(10);
return view('posts.search', compact('posts'));
}
我的路线:
Route::group(['prefix' => 'posts', 'as' => 'posts.'], function () {
Route::post('/search', ['as' => 'posts', 'uses' => 'SearchController@posts']);
});
我的观点是:
<form action="{{ route('searches.posts') }}" method="post">
{{ csrf_field() }}
<div class="input-field col s6">
<input class="input blue-text text-lighten-3" type="text" name="term">
</div>
<div class="input-field col s6">
<button type="submit">
<i class="material-icons">search</i>
</button>
</div>
</form>
我提交后调用此函数并获取包含与搜索相关的所有项目的新视图.到目前为止它工作,但如果我现在刷新页面,我会得到以下错误:
Symfony \ Component \ HttpKernel \ Exception \
MethodNotAllowedHttpException No message
解决方法:
您的表单方法是post,因此在您的初始请求中,HTTP方法是POST.刷新页面时,浏览器发出GET请求而不是POST请求.
确保您的路由已注册GET和POST请求:
Route::match(['get', 'post'], '/search', ['as' => 'posts', 'uses' => 'SearchController@search']);
https://laravel.com/docs/5.5/routing#basic-routing
您可能还想考虑将表单方法切换为get.这将使搜索参数显示在URL中,并使用户能够链接到搜索结果.
标签:php,search,laravel,symfony,laravel-5-5 来源: https://codeday.me/bug/20190701/1347895.html