Laravel分页漂亮的URL [英] Laravel pagination pretty URL

查看:123
本文介绍了Laravel分页漂亮的URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在Laravel 4中获得漂亮的分页URL?

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

例如,默认情况下:

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

我想得到什么:

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

此外,分页应该以这种方式呈现,并且在分页后面应以这种方式出现.

Also, the pagination should render this way, and appending to the pagination should appear in this way.

推荐答案

这是一个骇人的解决方法.我正在使用Laravel v4.1.23.假设页码是您网址的最后一位.尚未进行深入测试,因此我对人们可以找到的任何bug感兴趣.我对更好的解决方案更感兴趣:-)

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 :-)

路线:

Route::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);
});

查看:

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

型号:

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

迁移:

<?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分页漂亮的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆