Laravel 5路由分页url编码问题 [英] Laravel 5 route pagination url encoding issue

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

问题描述

我建立了一个laravel 5应用程序,现在我正在测试如何处理不同的输入。所以我遇到了一个奇怪的问题。在标题中我有一个搜索字段。它返回结果,以10为分页。



问题



如果用户输入一个字母,例如e英文,一切正常。然而,当用户输入一个字母时,对于保加利亚语中的e示例,结果的第一页正确显示,用户在第2页时,保加利亚语中的е搜索中的查询更改为%D0 %B5,没有更多的结果显示。这是网站的实际链接。 http://podobri.eu



我想这有东西与编码有关,但我看不到我做错了什么。



以下是实际代码



路线

  Route :: get('/ search',[
'使用'=>'\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% $ b - > orderBy('created_at','desc') - > paginate(10);

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

查看

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

<! - 分页 - >
{!! $ problems->追加(Request :: except('page')) - > render()!!}

搜索表单

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


解决方案

看起来像我的问题正在发生,因为paginator正在追加一个带有一些奇怪重定向的尾部斜杠(不知道你们是否使用自定义htaccess)。例如,如果您搜索e,这是URL:

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

但是,第二页的URL是这样的:

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

注意?查询之前的斜杠。如果你删除斜杠,它是有效的。那么,你如何解决这个问题?



这实际上是在几个月前固定的。您可以在此处看到此提交: https://github.com/laravel/framework/commit/806fb79f6e06f794349aab5296904bc2ebe53963



所以,如果你使用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' => '\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>

解决方案

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编码问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆