2

嗨,我正在学习如何设置搜索表单的教程,但出现route错误(NotFoundHttpException)。

形式

   {!! Form::open(['method'=>'GET' ,'url' => 'search', 'class'=>'form-group main-form', 'id'=>'search-form', 'role'=>'form']) !!}
      {{ csrf_field() }}
      <div style="display:none"><input name="utf8" type="hidden" value="✓"></div>
      <input class="form-group main-form" id="q_objname_en_cont" name="searchKey"  placeholder='Search by Job title' required="required" style="height:40px;width:60%" type="search">
      <input class="btn btn-warning" type="submit" value="Search">
   {!! Form::close() !!}

路线

//Search route(get)
Route::get('search/{searchkey}', 'EmploiController@search')->where('searchkey', '[A-Za-z]+')->name('search');

网址(浏览器)

http://localhost:8000/search?_token=LJpgN3AwCFoDElOkFsSOX8BBLU1IFOzMvUYiokQj&utf8=%E2%9C%93&searchKey=quia
4

2 回答 2

2

将您的路线更改为此。当您将搜索参数作为查询字符串传递时,您不需要 url 的第二部分。

Route::get('search', 'EmploiController@search')->name('search');

将您的表格更改为此。当您使用表单 GET 方法时,您不需要发送 csrf 令牌。

{!! Form::open(['method'=>'GET' ,'url' => 'search', 'class'=>'form-group main-form', 'id'=>'search-form', 'role'=>'form']) !!}
<input class="form-group main-form" id="q_objname_en_cont" name="searchKey"  placeholder='Search by Job title' required="required" style="height:40px;width:60%" type="search">
<input class="btn btn-warning" type="submit" value="Search">
{!! Form::close() !!}

我还删除了不需要的输入<input name="utf8" type="hidden" value="✓">。如果它是有意的,请将其添加回来。

于 2017-06-05T17:33:40.127 回答
0

我认为您所遵循的教程一切都有问题,没有安全问题,当然,这不是在 Laravel 中实现搜索的正确方法,您在对该路线的每个请求中都将您的 csrf_token 暴露给世界。

我对您的建议是阅读更好的资源并深入研究文档。

https://laracasts.com/discuss/channels/laravel/search-option-in-laravel-5?page=1

于 2017-06-05T17:42:52.017 回答