有没有办法在 Laravel 4 中获取分页漂亮的 URL?
例如,默认情况下:
http://example.com/something/?page=3
我想得到的是:
http://example.com/something/page/3
此外,分页应该以这种方式呈现,并且附加到分页应该以这种方式出现。
最佳答案
这是一个 hacky 解决方法。我正在使用 Laravel v4.1.23。它假定页码是您网址的最后一位。还没有对其进行深入测试,所以我对人们可以找到的任何错误感兴趣。我对更好的解决方案更感兴趣:-)
路线:
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');
}
}
关于php - Laravel 分页漂亮的 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20974404/