Laravel 5 Paginator 生成链接的问题 [英] Problems of links generated by Laravel 5 Paginator
问题描述
我在 Laravel 5 中尝试使用分页器时遇到了一个奇怪的问题.数据和分页信息已准备好,但是当我在刀片中调用 $model->render() 时,页面链接完全错误.
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.
这是控制器中的一些示例代码:
Here is some sample code in controller:
public function index()
{
$articles = Article::latest('published_at')->paginate(3);
return view('articles/index')->with('articles',$articles);
}
刀片中的代码:
{!! $articles->render() !!}
最后是路由中的代码:
Route::get('articles',array('as' => 'article-list','uses' => 'ArticleController@index'));
问题是 Laravel 为不同的页面生成了错误的 url,例如:example.com/articles/?page=2,在 ? 之前添加了/.
The problem is Laravel generates wrong urls to different pages as such : example.com/articles/?page=2, with additional / before ?.
有一种解决方法可以通过在将数据传递给视图之前调用 setPath() 来更正 url,并且链接现在可以工作,如下所示:
There is a workaround to correct the url by calling setPath() before passing data to view, and links now work, like this:
$articles = Article::latest('published_at')->paginate(3);
$articles->setPath('articles');
return view('articles/index')->with('articles',$articles);
但是有没有其他选项可以在 Laravel 5 中生成正确的页面链接而我错过了什么?
But are there other options to generate correct links to pages in Laravel 5 and I missed something?
谢谢.
环境更新:xampp.
Update on environment: xampp.
推荐答案
在您的刀片中使用此代码,
Use this code in your blade,
{!! str_replace('/?', '?', $articles->render()) !!}
此代码生成正确的网址.
This code generate the correct url.
这篇关于Laravel 5 Paginator 生成链接的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!