2016-02-04 60 views
3

我構建了一個laravel 5應用程序,現在我正在測試它如何處理不同的輸入。因此我遇到了一個奇怪的問題。在標題中我有一個搜索字段。如果用戶輸入一個字母,在英語的例子「E」,它返回的結果,由10Laravel 5路由分頁url編碼問題

問題

分頁,一切都運行得很好。但是,當用戶輸入一個字母時,例如保加利亞語中的「e」 - 結果的第一頁顯示正確,當用戶點擊第2頁時,保加利亞語中從「е」開始的查詢變爲「%D0 %B5「並且沒有顯示更多結果。這是一個到網站的實際鏈接。 http://podobri.eu

我想這與編碼有關,但我看不出我做錯了什麼。

下面是實際的代碼

路線

Route::get('/search', [ 
    'uses' => '\Podobri\Http\Controllers\[email protected]', 
    'as'=>'search.results', 
]); 

SearchController

public function getResults(Request $request){ 

     $query = $request->input('query'); 
     $comments = Comment::where(function($query){ 
      return $query; 
     })->orderBy('created_at', 'desc')->get(); 

     if(!$query || $query==''){ 
      return view('problems.index')->with('comments', $comments); 
     } 

     $problems = Problem::where(DB::raw("CONCAT(problem_title, ' ', problem_description)"), 'LIKE', "%$query%") 
       ->orWhere('location', 'LIKE', "%$query%") 
       ->orWhere('category', 'LIKE', "%$query%") 
       ->orderBy('created_at', 'desc')->paginate(10); 

     Carbon::setLocale('bg'); 
     return view('search.results') 
       ->with('comments', $comments) 
       ->with('problems', $problems) 
       ->with('title', 'Резултати за "'."$query".'" | Подобри') 
       ->with('description', 'Резултати за "'."$query".'" в системата на Подобри'); 
    } 

查看

 @foreach($problems as $problem) 
      <div> 
       @include('problems.partials.problemblock') 
      </div> 
     @endforeach 

     <!-- Paginating--> 
     {!! $problems->appends(Request::except('page'))->render() !!} 

的搜索形式

<form action="{{ route('search.results') }}" role="search" class="navbar-form navbar-left head-form-responsive"> 
        <div class="form-group"> 
         <input type="text" required id='searchQuery' title="Търсете за проблеми" value="{{ Request::input('query') }}" name="query" class="form-control" 
           placeholder="Търсете за проблеми"/> 
        </div> 
        <button type="submit" id='searchBtn' class="btn btn-default">Търсете</button> 
       </form> 
+0

當你打第2頁這個動作方法是相同的,其處理數據? –

+0

我不清楚你的意思。我做了一些認爲不好的習慣嗎? –

+0

不,我的意思是這個方法getResults()和page2一樣嗎? –

回答

4

它看起來對我來說,您的問題正在發生,因爲分頁程序被附加在末尾加上斜槓用一些奇怪的重定向(不知道,如果你們使用自定義的htaccess)。例如,如果你搜索E,這是網址:

http://podobri.eu/search?query=e 

然而,對於第二個頁面的URL是這樣的:

http://podobri.eu/search/?query=e&page=2 

公告中的?query前面的斜線。如果你刪除斜槓,它的工作原理。那麼,你如何解決這個問題?

這實際上是在幾個月前修復的。你可以在這裏看到這個提交:https://github.com/laravel/framework/commit/806fb79f6e06f794349aab5296904bc2ebe53963

所以,如果你使用L5.1或5.2,你可以運行composer update,它會自行修復。但是,如果您使用的是5.0,現在看來似乎仍然有這個bug,因此你可以使用setPath方法,並嘗試這個:

{!! $problems->setPath('')->appends(Request::except('page'))->render() !!} 
+0

我正在使用Laravel 5.0。試過你的解決方案。像魅力一樣工作。謝謝。 –