关于php:Laravel-在首页上显示一些帖子,然后链接到其余部分

Laravel - show few posts on homepage, then link to rest

我试图在Laravel中创建一个简单的博客,但我对这个小细节深有感触。我有博客文章,并希望在首页上显示很少内容,并链接到路由localhost / posts上的其他页面。

问题是我不知道如何从首页创建到分页帖子的链接,所以分页从首页的最后一篇帖子结束的地方开始。

编辑:我希望用户能够单击路线"帖子"并查看所有帖子,甚至是首页中的帖子

示例

1
2
localhost/  - has first 3 posts
localhost/posts?page=2  - has the rest starting from 4th post

我已经尝试过这种方法,但无济于事。

路线

1
Route::get('posts?page={page}', ['as' => 'rest', 'uses' => 'Controller@getRest']);

控制器具有此功能

1
2
3
4
5
6
public function getRest($page) {
    Paginator::setCurrentPage($page);
    $posts = Post::paginate(3);

    return View::make('posts')->with('posts', $posts);
}

我试图像这样在主页视图模板中创建链接:

1
Show the rest of posts

谢谢您的帮助。


这应该有效。唯一的问题是,当用户单击主页上的page 2链接时,用户将看到10个帖子,从第13个帖子开始,而不是第3个帖子。虽然将Controller::posts中的->skip(3 + ($page - 1) * 10)更改为->skip(3 + ($page - 2) * 10)似乎可以解决问题,但page 1链接将失败。

路线

1
2
Route::get('/', ['as' => 'home', 'uses' => 'Controller@home' ]);
Route::get('posts', ['as' => 'posts', 'uses' => 'Controller@posts' ]);

控制器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class Controller extends BaseController {

    public function home()
    {
        $posts = Post::take(3)->get();
        $pagination = Paginator::make($posts->toArray(), Post::count(), 10);

        $pagination->setBaseUrl(route('posts'));

        return View::make('home', compact('posts', 'pagination'));
    }

    public function posts()
    {
        // Current page number (defaults to 1)
        $page = Input::get('page', 1);

        // Get 10 post according to page number, after the first 3
        $posts = Post::skip(3 + ($page - 1) * 10)->take(10)->get();

        // Create pagination
        $pagination = Paginator::make($posts->toArray(), Post::count(), 10);

        return View::make('posts', compact('posts', 'pagination'));
    }

}

home.blade.php

1
2
3
4
5
6
7
@foreach ($posts as $post)

     {{ $post->title}}

@endforeach

{{ $pagination->links() }}