dev-master
9999999-devBeautiful urls for Laravel pagination
MIT
The Requires
by Sercan Çakır
laravel eloquent pagination
Wallogit.com
2017 © Pedro Peláez
Beautiful urls for Laravel pagination
Create beautiful and SEO friendly links for pagination., (*1)
First, you'll need to add the package to the require attribute of your composer.json file:, (*2)
{
"require": {
"mayoz/pagination": "dev-master"
},
}
Afterwards, run composer update from your command line.
And finaly, open config/app.php. Find 'Illuminate\Pagination\PaginationServiceProvider' and replace with 'Mayoz\Pagination\PaginationServiceProvider' in providers array., (*3)
This is very easy. Consider the following examples. I accept to have your model. Create route, controller and view., (*4)
Define route in app\Http\routes;, (*5)
<?php
$router->get('articles/{page?}', [
'as' => 'articles',
'uses' => 'HomeController@articles'
]);
Cool. Don't write extra code!, (*6)
<?php namespace App\Http\Controllers;
use App\Article;
class HomeController {
public function articles()
{
$articles = Article::paginate(5);
return view('articles', compact('articles'));
}
}
Core pagination class not changed. Can use all the features offered by Laravel., (*7)
You also use;
- {!! $articles->appends([ 'sort' => 'vote' ])->links() !!}
- {!! $articles->links('pagination.special') !!}
- or future methods..., (*8)
The following outputs are generated. I think that's enough examples. As possible, SEO friendly. Nevertheless, please check your url. Use canonical metadata if necessary., (*9)
Use such as {page?}, if page pattern at the end of the route., (*10)
<?php
$router->get('articles/{page?}', [
'as' => 'articles',
'uses' => 'HomeController@articles'
]);
Outputs: - /articles - /articles/2 - /articles/3, (*11)
If {page} pattern in the middle of the route, no need to question mark., (*12)
<?php
$router->get('module/{page}/articles', [
'as' => 'module.articles',
'uses' => 'HomeController@articles'
]);
Outputs: - /module/1/articles - /module/2/articles - /module/3/articles, (*13)
If you need to use another route instead of the current route, use the route() method. This method accepts the route name and route arguments., (*14)
NOTE: Second arg is optional. This arg for special routes parameters., (*15)
Routes is different but used same controller and view., (*16)
<?php
$router->get('articles', [
'as' => 'articles',
'uses' => 'HomeController@articles'
]);
$router->get('articles/{module}/{page?}', [
'as' => 'articles.paginate',
'uses' => 'HomeController@articles'
]);
The same as the previous definition. Added special route (its name article.paginate) for pagination url builder. This route required one parameter (module) except {page}., (*17)
NOTE: Set automatically route parameters on current route., (*18)
<?php namespace App\Http\Controllers;
use App\Article;
class HomeController {
public function articles()
{
$articles = Article::paginate(5);
$articles->route('articles.paginate', [ 'module' => 'photo' ]);
return view('articles', compact('articles'));
}
}
Outputs: - /articles/photo - /articles/photo/2 - /articles/photo/3, (*19)
Install and use this package. If find bug, fix and send a pull request on the develop branch., (*20)
This package was written by Sercan Çakır. It released under the MIT License. See the LICENSE file for details., (*21)
Beautiful urls for Laravel pagination
MIT
laravel eloquent pagination