Laravel 5 Paginator 生成链接的问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28424522/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 10:53:12  来源:igfitidea点击:

Problems of links generated by Laravel 5 Paginator

laravelpaginationlaravel-5

提问by Carter

I came across a weird problem when I tried to use Paginator in Laravel 5. The data and pagination information were prepared, but when I called $model->render() in blade the links to pages were simply wrong.

我在 Laravel 5 中尝试使用分页器时遇到了一个奇怪的问题。数据和分页信息都准备好了,但是当我在刀片中调用 $model->render() 时,页面链接完全错误。

Here is some sample code in controller:

这是控制器中的一些示例代码:

public function index()
{
    $articles = Article::latest('published_at')->paginate(3);
    return view('articles/index')->with('articles',$articles);
}

And the code in blade:

刀片中的代码:

{!! $articles->render() !!}

Lastly the code in routes:

最后是路由中的代码:

Route::get('articles',array('as' => 'article-list','uses' => 'ArticleController@index'));

The problem is Laravel generates wrong urls to different pages as such : example.com/articles/?page=2, with additional / before ?.

问题是 Laravel 为不同的页面生成了错误的 url,例如:example.com/articles/? page=2 ,在 ? 之前添加了 /。

There is a workaround to correct the url by calling setPath() before passing data to view, and links now work, like this:

有一种解决方法可以通过在将数据传递给视图之前调用 setPath() 来更正 url,并且链接现在可以工作,如下所示:

$articles = Article::latest('published_at')->paginate(3);
$articles->setPath('articles');
return view('articles/index')->with('articles',$articles);

But are there other options to generate correct links to pages in Laravel 5 and I missed something?

但是有没有其他选项可以在 Laravel 5 中生成正确的页面链接而我错过了什么?

Thank you.

谢谢你。



Update on environment: xampp.

环境更新:xampp。

回答by Vinod VT

Use this code in your blade,

在您的刀片中使用此代码,

{!! str_replace('/?', '?', $articles->render()) !!}

This code generate the correct url.

此代码生成正确的网址。