2017 © Pedro Peláez
 

library pagination

Beautiful urls for Laravel pagination

image

mayoz/pagination

Beautiful urls for Laravel pagination

  • Friday, November 6, 2015
  • by mayoz
  • Repository
  • 2 Watchers
  • 9 Stars
  • 91 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 5 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Laravel Pagination

Create beautiful and SEO friendly links for pagination., (*1)


Installation and Requirements

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)

Using

This is very easy. Consider the following examples. I accept to have your model. Create route, controller and view., (*4)

Route

Define route in app\Http\routes;, (*5)

<?php
$router->get('articles/{page?}', [
    'as'   => 'articles',
    'uses' => 'HomeController@articles'
]);

Controller

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'));
    }
}

View

Core pagination class not changed. Can use all the features offered by Laravel., (*7)



    @foreach ($articles as $article)
  • {{ $article->title }}
  • @endforeach
{!! $articles->links() !!}

You also use; - {!! $articles->appends([ 'sort' => 'vote' ])->links() !!} - {!! $articles->links('pagination.special') !!} - or future methods..., (*8)

Output examples

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)

Using special route

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

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'
]);

Controller

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)

Contribute

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)

The Versions

06/11 2015

dev-master

9999999-dev

Beautiful urls for Laravel pagination

  Sources   Download

MIT

The Requires

 

by Sercan Çakır

laravel eloquent pagination