沃梦达 / 编程问答 / php问题 / 正文

Laravel 5 路由分页 url 编码问题

Laravel 5 route pagination url encoding issue(Laravel 5 路由分页 url 编码问题)

本文介绍了Laravel 5 路由分页 url 编码问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我构建了一个 Laravel 5 应用程序,现在我正在测试它如何处理不同的输入.因此我遇到了一个奇怪的问题.在标题中,我有一个搜索字段.它返回结果,按 10 分页.

问题

如果用户输入一个字母,例如英语中的e",则一切正常.但是,当用户输入字母时,例如保加利亚语中的e" - 结果的第一页显示正确,当用户点击第 2 页时,搜索中的查询从保加利亚语中的е"变为%D0%B5" 并且不再显示结果.这是该网站的实际链接.http://podobri.eu

我想这与编码有关,但我看不出我做错了什么.

这里是实际代码

路线

Route::get('/search', ['使用' =>'PodobriHttpControllersSearchController@getResults','as'='=>'search.results',]);

搜索控制器

公共函数 getResults(Request $request){$query = $request->input('query');$comments = Comment::where(function($query){返回 $query;})->orderBy('created_at', 'desc')->get();if(!$query || $query==''){return view('problems.index')->with('comments', $comments);}$problems = Problem::where(DB::raw("CONCAT(problem_title, ' ', question_description)"), 'LIKE', "%$query%")->orWhere('location', 'LIKE', "%$query%")->orWhere('category', 'LIKE', "%$query%")-> orderBy('created_at', 'desc')-> paginate(10);Carbon::setLocale('bg');返回视图('搜索结果')->with('comments', $comments)->with('问题', $problems)->with('title', 'Резултатиза "'."$query".'" | Подобри')->with('description', 'Резултати за "'."$query".'" в системата на Подобри');}

查看

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

@endforeach<!-- 分页-->{!!$problems->appends(Request::except('page'))->render() !!}

搜索表单

<button type="submit" id='searchBtn' class="btn btn-default">Търсете</button></表单>

解决方案

在我看来,您的问题正在发生,因为分页器附加了带有一些奇怪重定向的尾部斜杠(不确定你们是否使用自定义 htaccess).例如,如果您搜索 e,这是 URL:

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

然而,第二页的 URL 是这样的:

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

注意 ?query 前面的斜线.如果删除斜杠,它会起作用.那么,你如何解决这个问题?

这实际上是在几个月前修复的.您可以在此处查看此提交:https://github.com/laravel/framework/commit/806fb626904b95c5c95c5c5c95c5c95c5c95c5296904/a>

因此,如果您使用的是 L5.1 或 5.2,您可以运行 composer update,它会自行修复.但是,如果您使用的是 5.0,它似乎仍然存在此错误,因此您可以使用 setPath 方法并尝试以下操作:

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

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' => 'PodobriHttpControllersSearchController@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>

解决方案

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() !!}

这篇关于Laravel 5 路由分页 url 编码问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Laravel 5 路由分页 url 编码问题

基础教程推荐