关于 laravel 分页 seo浅谈

秦城
2023-12-01

本次是使用的laravel6.1

安装扩展:laravel-paginateroute

第一步:composer require spatie/laravel-paginateroute

第二步:注册    // config/app.php

'providers' => [ ...

Spatie\PaginateRoute\PaginateRouteServiceProvider::class,

];

'aliases'=>[ ...

'PaginateRoute' => Spatie\PaginateRoute\PaginateRouteFacade::class,

];

执行命令:

php artisan vendor:publish --provider="Spatie\PaginateRoute\PaginateRouteServiceProvider"

第三步:// app/Providers/RouteServiceProvider.php

//引用

use PaginateRoute;

// ...

public function boot(Router $router) {

//使用

PaginateRoute::registerMacros();

parent::boot();

}

使用

在路由中

// routes/home.php

Route::paginate('/knowledge', 'IndexController@knowledge');

这样会生成两条路由

/knowledge

/knowledge/page/{page}

//在控制其中使用 simplePaginate

$news = DB::table('sys_home_news')->where('stated',1)->simplePaginate(6);

//在页面中分页显示

1、上一页 下一页

@if(PaginateRoute::hasPreviousPage()) <a href="{{ PaginateRoute::previousPageUrl() }}">Previous</a> @endif @if(PaginateRoute::hasNextPage($news )) <a href="{{ PaginateRoute::nextPageUrl($news ) }}">Next</a> @endif

2、列表形式 这是在github上找的

$this->app['router']->paginate('dummies', function () {

$dummies = Dummy::paginate(5);

$paginateRoute = $this->app['paginateroute'];

return [

'list' => $paginateRoute->renderPageList($dummies),

'listClass' => $paginateRoute->renderPageList($dummies, false, 'pagination'),

];

});

<!-- Example output:输出示例 -->

<ul class="pagination">

<li><a href="http://example.com/news">1</a></li>

<li><a href="http://example.com/news/page/2">2</a></li>

<li class="active"><a href="http://example.com/news/page/3">3</a></li>    <li><a href="http://example.com/news/page/4">4</a></li>

<li><a href="http://example.com/news/page/4">&raquo;</a></li>

</ul>

示例1:https://zhuanlan.zhihu.com/p/130363516

示例2:https://github.com/spatie/laravel-paginateroute/tree/master

 类似资料: