Laravel 默认的分页,需要 url 中 是附带参数 page。对SEO 不佳:
默认:https://www.test.com/php8?page=2
变成:https://www.test.com/php8_2
Laravel 技术文档,写不够清楚,产生了误解。看到很多人都自定义函数来是实现。
其实,Laravel 本身是支持此功能的,是能实现。
第一步:配置路由。
Route::get('/{dir?}/{cid?}', [Home\TopicController::class,'channel'])->where(['dir'=>'[A-Za-z0-9]+','cid'=>'[0-9]+']);
Route::get('/{dir?}/{cid?}_{page}', [Home\TopicController::class,'channel'])->where(['dir'=>'[A-Za-z0-9]+','cid'=>'[0-9]+','page'=>'[0-9]+']);
规则中的 {dir} 接收 php8 频道字符串,{page} 接收 页面数字
第二步:控制器中,那传过来的参数 page 写入 paginate 分页器中。
public function channel(Request $request,$dir,$cid,$page=1)
{
$nextPage=$page+1;
$prePage=$page-1;
if($prePage<1)$prePage=1;
$limit=20;
$topicList=DB::tabl