Laravel 5 Paginator生成的链接问题 [英] Problems of links generated by Laravel 5 Paginator
问题描述
当我尝试在Laravel 5中使用Paginator时遇到一个奇怪的问题. 数据和分页信息已经准备好了,但是当我在刀片中调用$ 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() !!}
最后一条路线中的代码:
Lastly the code in routes:
Route::get('articles',array('as' => 'article-list','uses' => 'ArticleController@index'));
问题是Laravel生成了错误的URL,这些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.
推荐答案
在刀片中使用此代码,
{!! str_replace('/?', '?', $articles->render()) !!}
此代码生成正确的网址.
This code generate the correct url.
这篇关于Laravel 5 Paginator生成的链接问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!