Laravel 5 route pagination url encoding issue

2019-02-13 23:07发布

问题:

I built a laravel 5 application and now I am testing how it handles different inputs. Thus I encountered a weird problem. In the header I have a search field. It returns results, paginated by 10.

The problem

If a user inputs a letter, for an example "e" in English, everything works just fine. However, when a user enters a letter, for an example "e" in Bulgarian - the first page of the results is shown correctly and when a user hits page 2 the query in the search from "е" in Bulgarian changes to "%D0%B5" and no more results are shown. Here is an actual link to the website. http://podobri.eu

I guess this has something to do with the encoding but I can't see what I am doing wrong.

Here is the actual code

Route

Route::get('/search', [
   'uses' => '\Podobri\Http\Controllers\SearchController@getResults',
    '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".'" в системата на Подобри');
    }

View

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

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

Search form

<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>

回答1:

It looks to me like your issue is happening because the paginator is appending a trailing slash with some odd redirect (not sure if you guys are using custom htaccess). Example, if you search for e, this is the URL:

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

However, the URL for the second page is this:

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

Notice the slash in front of ?query. If you remove the slash, it works. So, how can you fix this?

This was actually fixed a few months ago. You can see this commit here: https://github.com/laravel/framework/commit/806fb79f6e06f794349aab5296904bc2ebe53963

So, if you are using L5.1 or 5.2, you can run composer update, and it'll fix itself. However, if you are using 5.0, it seems like it still has this bug so you can use the setPath method and try this instead:

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


回答2:

I had a similar problem and my solution was changed the method of the route.

Route::post('uri', 'Controller@function')
    ->name ('view.function');

for:

Route::any('uri', 'Controller@function')
    ->name ('view.function');

It's works for me.

Regards and good luck.