Laravel pagination pretty URL
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:
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);
});
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 Pagination with pretty urls than query string
Okay problem solved , now pretty pagination urls for Laravel is working and here is the solution.
i added the getByPage method into the relevant model class posted in the following link (Credit goes to him)
and called $this->user->getByPage($page, $limit);
in the Routes ,
There we have Pretty pagination URLs !
Related Topics
Get Min and Max Value in PHP Array
Understanding PHP & (Ampersand, Bitwise And) Operator
How to Check If a Video Exists on Youtube, Using PHP
How to Integrate Nodejs + Socket.Io and PHP
How to Add New Column to MySQL Table
PHP Combine Two Associative Arrays into One Array
Scandir() to Sort by Date Modified
Deep Recursive Array of Directory Structure in PHP
Ternary Operator Left Associativity
What Can Use for Datetime::Diff() for PHP 5.2
Setting Value of a HTML Form Textarea
Correct Way to Set Bearer Token with Curl
Laravel Mail::Send() Sending to Multiple to or Bcc Addresses