Skip to content

Commit

Permalink
PaginateRoute
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastiandedeyne committed Jun 3, 2015
1 parent 3904428 commit ea444ec
Show file tree
Hide file tree
Showing 13 changed files with 173 additions and 116 deletions.
25 changes: 0 additions & 25 deletions .travis.yml

This file was deleted.

20 changes: 3 additions & 17 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,6 @@
# Changelog

All Notable changes to `:package_name` will be documented in this file
All Notable changes to `spatie/laravel-paginateroute` will be documented in this file

## NEXT - YYYY-MM-DD

### Added
- Nothing

### Deprecated
- Nothing

### Fixed
- Nothing

### Removed
- Nothing

### Security
- Nothing
## 2015-06-03
- First release!
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Contributions are **welcome** and will be fully **credited**.

We accept contributions via Pull Requests on [Github](https://github.com/thephpleague/:package_name).
We accept contributions via Pull Requests on [Github](https://github.com/spatie/laravel-paginateroute).


## Pull Requests
Expand Down
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# The MIT License (MIT)

Copyright (c) 2015 :author_name <:author_email>
Copyright (c) 2015 Sebastian De Deyne <[email protected]>

> Permission is hereby granted, free of charge, to any person obtaining a copy
> of this software and associated documentation files (the "Software"), to deal
Expand Down
78 changes: 61 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,42 +1,86 @@
# :package_name
# Laravel Paginate Route

[![Latest Version on Packagist](https://img.shields.io/packagist/v/league/:package_name.svg?style=flat-square)](https://packagist.org/packages/league/:package_name)
[![Latest Version on Packagist](https://img.shields.io/packagist/v/spatie/laravel-paginateroute.svg?style=flat-square)](https://packagist.org/packages/spatie/laravel-paginateroute)
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md)
[![Build Status](https://img.shields.io/travis/thephpleague/:package_name/master.svg?style=flat-square)](https://travis-ci.org/thephpleague/:package_name)
[![Coverage Status](https://img.shields.io/scrutinizer/coverage/g/thephpleague/:package_name.svg?style=flat-square)](https://scrutinizer-ci.com/g/thephpleague/:package_name/code-structure)
[![Quality Score](https://img.shields.io/scrutinizer/g/thephpleague/:package_name.svg?style=flat-square)](https://scrutinizer-ci.com/g/thephpleague/:package_name)
[![Total Downloads](https://img.shields.io/packagist/dt/league/:package_name.svg?style=flat-square)](https://packagist.org/packages/league/:package_name)
[![Build Status](https://img.shields.io/travis/thephpspatie/laravel-paginateroute/master.svg?style=flat-square)](https://travis-ci.org/thephpspatie/laravel-paginateroute)
[![Coverage Status](https://img.shields.io/scrutinizer/coverage/g/thephpspatie/laravel-paginateroute.svg?style=flat-square)](https://scrutinizer-ci.com/g/thephpspatie/laravel-paginateroute/code-structure)
[![Quality Score](https://img.shields.io/scrutinizer/g/thephpspatie/laravel-paginateroute.svg?style=flat-square)](https://scrutinizer-ci.com/g/thephpspatie/laravel-paginateroute)
[![Total Downloads](https://img.shields.io/packagist/dt/spatie/laravel-paginateroute.svg?style=flat-square)](https://packagist.org/packages/spatie/laravel-paginateroute)

**Note:** Replace ```:author_name``` ```:author_username``` ```:author_website``` ```:author_email``` ```:package_name``` ```:package_description``` with their correct values in [README.md](README.md), [CHANGELOG.md](CHANGELOG.md), [CONTRIBUTING.md](CONTRIBUTING.md), [LICENSE.md](LICENSE.md) and [composer.json](composer.json) files, then delete this line.
This package adds the `paginate` route method to support pagination via custom routes instead of query strings. This also allows for easily translatable pagination routes ex. `/news/page/2`, `/nieuws/pagina/2`.

This is where your description should go. Try and limit it to a paragraph or two, and maybe throw in a mention of what
PSRs you support to avoid any confusion with users and contributors.
This is currently a relatively small package that doesn't cover all use cases, make sure you check out the limitations section of this readme.

## Install

Via Composer

``` bash
$ composer require league/:package_name
$ composer require spatie/laravel-paginateroute
```

First register the service provider in your application.

``` php
// config/app.php

'providers' => [
...
'Spatie\PaginateRoute\PaginateRouteServiceProvider',
];
```

Then register the macros in `App\Providers\RouteServiceProder::boot()`.

``` php
// app/Providers/RouteServiceProvider.php

public function boot(Router $router)
{
$this->app['paginateroute']->registerMacros();

parent::boot($router);
}
```

## Usage

The `paginate` route macro will register two routes for you.

``` php
$skeleton = new League\Skeleton();
echo $skeleton->echoPhrase('Hello, League!');
// app/Http/routes.php

// Generates /users & /users/page/{page}
Route::paginate('users', 'UsersController@index');

```

## Change log
In your route's action you can just use laravel's regular pagination methods.

Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.
``` php
// app/Http/Controllers/UsersController.php

## Testing
public function index()
{
return view('users.index', ['users' => \App\User::simplePaginate(5)]);
}
```

If you want to customize or add translations for the "page" url segment, you can publish the language files.

``` bash
$ composer test
$ art vendor:publish --provider="Spatie\PaginateRoute\PaginateRouteServiceProvider"
```


## Limitations

Laravel's paginator url functions still returns the default query-string version of the url, so you'll need to add some sort of logic to your controller action or views to generate next page, previous page and other urls.

## Changelog

Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.

## Contributing

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
Expand All @@ -47,7 +91,7 @@ If you discover any security related issues, please email :author_email instead

## Credits

- [:author_name](https://github.com/:author_username)
- [Sebastian De Deybe](https://github.com/sebastiandedeyne)
- [All Contributors](../../contributors)

## License
Expand Down
29 changes: 0 additions & 29 deletions phpunit.xml.dist

This file was deleted.

7 changes: 7 additions & 0 deletions resources/lang/en/paginateroute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

return [

'page' => 'page',

];
7 changes: 7 additions & 0 deletions resources/lang/fr/paginateroute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

return [

'page' => 'page',

];
7 changes: 7 additions & 0 deletions resources/lang/nl/paginateroute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

return [

'page' => 'pagina',

];
30 changes: 30 additions & 0 deletions src/PaginateRoute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Spatie\PaginateRoute;

use Route;

class PaginateRoute
{
/**
* Register the Route::paginate macro
*
* @return void
*/
public function registerMacros()
{
// Unfortunately we can't do this in the service provider since routes are booted first
app('translator')->addNamespace('paginateroute', __DIR__.'/../resources/lang');

$pageName = trans('paginateroute::paginateroute.page');

Route::macro('paginate', function ($uri, $action) use ($pageName) {
Route::group(
['middleware' => 'Spatie\PaginateRoute\SetPageMiddleware'],
function () use ($pageName, $uri, $action) {
Route::get($uri, $action);
Route::get($uri.'/'.$pageName.'/{page}', $action)->where('page', '[0-9]+');
});
});
}
}
30 changes: 30 additions & 0 deletions src/PaginateRouteServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Spatie\PaginateRoute;

use Illuminate\Support\ServiceProvider;

class PaginateRouteServiceProvider extends ServiceProvider
{
/**
* Perform post-registration booting of services.
*
* @return void
*/
public function boot()
{
$this->publishes([
__DIR__.'/../resources/lang' => base_path('resources/lang/vendor/paginateroute'),
]);
}

/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->app->instance('paginateroute', new PaginateRoute);
}
}
26 changes: 26 additions & 0 deletions src/SetPageMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Spatie\PaginateRoute;

use Closure;
use Illuminate\Pagination\Paginator;
use Route;

class SetPageMiddleware
{
/**
* Set the current page based on the page route parameter before the route's action is executed.
*
* @return \Illuminate\Http\Request
*/
public function handle($request, Closure $next)
{
$page = Route::getCurrentRoute()->parameter('page', 1);

Paginator::currentPageResolver(function () use ($page) {
return $page;
});

return $next($request);
}
}
26 changes: 0 additions & 26 deletions src/SkeletonClass.php

This file was deleted.

0 comments on commit ea444ec

Please sign in to comment.