agelxnash/seopagination

Laravel SEO Pagination

5.2.2 2017-05-03 21:46 UTC

README

此扩展包用于 Laravel 的分页。使用它,您可以防止空白页的打开。例如,您的分页只有 10 页,但用户请求了编号为 101 的页面。使用此扩展包,用户将被重定向到最后(10)或第一页...或者发送 404 错误。

最重要的是,分页器现在不包含对第一页的 GET 变量的引用。

旧版

http://example.com/news?page=1
http://example.com/news?page=2
http://example.com/news?page=3
...
etc.

新版本

http://example.com/news
http://example.com/news?page=2
http://example.com/news?page=3
...
etc.

安装

步骤 1

composer require agelxnash/seopagination:5.2.*@dev

步骤 2

一旦安装了 SEOPagination,您需要将服务提供程序注册到应用程序中。打开 config/app.php 并将 providers 键的 Illuminate\Pagination\PaginationServiceProvider 替换为

AgelxNash\SEOPagination\PaginationServiceProvider::class

配置

您需要运行以下命令以将配置发布到您的应用程序,否则在包更新时将被覆盖。

php artisan vendor:publish --provider="AgelxNash\SEOPagination\PaginationServiceProvider"

现在您可以编辑 config/seo-pagination.php 文件

action_on_error

  • first (发送重定向到第一个分页页面,带有错误状态码的响应状态码)
  • out (发送重定向到末尾分页页面,带有错误状态码的响应状态码)
  • abort (返回 404 错误。不使用错误状态码)

error_status

任何响应状态码。例如 - 307 (默认值)或 301

使用方法

将特性添加到您的模型中

use Illuminate\Database\Eloquent\Model;

Class Post extends Model{
	use \AgelxNash\SEOPagination\Eloquent\ReplaceBuilder
}

在调用 paginate() 方法之后,您可以在 checkPaginate() 方法中检查 data 变量。结果将是对象 \Illuminate\Http\RedirectResponsetrue。请参阅带有检查分页的控制器示例方法

public function example()
{
	$posts = Post::->orderBy('created_at', 'DESC')->paginate(10);
	if(($out = $posts->checkPaginate()) === true){
		$out = View::make('index', array('data'=> $posts));
	}
	return $out;
}