Is there a way to get a pagination pretty URL in Laravel 4?

For example, by default:

http://example.com/something/?page=3

And what I would like to get:

http://example.com/something/page/3

此外,分页应该以这种方式呈现,并且分页的附加应该以这种方式显示.

推荐答案

Here's a hacky workaround. I am using Laravel v4.1.23. It assumes page number is the last bit of your url. Haven't tested it deeply so I'm interested in any bugs people can find. I'm even more interested in a better solution :-)

路由:

路由::get('/articles/page/{page_number?}', function($page_number=1){
    $per_page = 1;
    Articles::resolveConnection()->getPaginator()->setCurrentPage($page_number);
    $articles = Articles::orderBy('created_at', 'desc')->paginate($per_page);
    return View::make('pages/articles')->with('articles', $articles);
});

View:

<?php
    $links = $articles->links();
    $patterns = array();
    $patterns[] = '/'.$articles->getCurrentPage().'\?page=/';
    $replacements = array();
    $replacements[] = '';
    echo preg_replace($patterns, $replacements, $links);
?>

Model:

<?php
class Articles extends Eloquent {
    protected $table = 'articles';
}

Migration:

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateArticlesTable extends Migration {

    public function up()
    {
        Schema::create('articles', function($table){
            $table->increments('id');
            $table->string('slug');
            $table->string('title');
            $table->text('body');
            $table->timestamps();
        });
    }

    public function down()
    {
        Schema::drop('articles');
    }
}

Laravel相关问答推荐

Inertia React中的错误文件上传更新

Laravel REST API:如何将数据库列名与参数的另一个名称映射?

Laravel Horizo​​n 限制与优化

如何使用动态翻译键翻译 Laravel硬编码字符串(还有 1 个错误)..(验证异常类)

Laravel Livewire 组件属性在 div 类中为空

覆盖 HTTP 标头的默认设置 (X-FRAME-OPTIONS)

Lumen 中的自定义 404 页面

PHP Laravel:如何设置或获取会话数据?

Homestead:文件夹映射到错误的文档根目录

CRON 作业(job) - LARAVEL - 输出

如何在 AWS Elastic Beanstalk 上设置和使用 Laravel 调度?

如何在没有 Artisan 的情况下运行 Laravel?

Laravel db 迁移 - renameColumn 错误 - 请求了未知的数据库类型枚举

升级到 laravel 5.3 后错误 datetime format: 1292 Incorrect datetime value: '0000-00-00 00:00:00'

Composer RuntimeException - 无法加载软件包 mews/purifier

assets资源不引用公用文件夹(Laravel)

Laravel hasManyThrough

在 laravel 的自定义路径中创建模型

Laravel 5.4 Vue.JS 无法挂载组件:未定义模板或渲染函数

Laravel 删除集合中的第一项