├── .gitignore ├── LICENSE.md ├── README.md ├── composer.json └── src ├── Http └── Middleware │ └── SetPrettyPagination.php ├── Pagination ├── GeneratePrettyUrl.php ├── LengthAwarePaginator.php └── Paginator.php ├── Providers └── PrettyPaginationProvider.php └── Routing └── Route.php /.gitignore: -------------------------------------------------------------------------------- 1 | /.idea 2 | /vendor 3 | composer.lock 4 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) CTSoft 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Laravel Pretty Pagination 2 | 3 | This package generates pretty pagination URLs: 4 | 5 | ``` 6 | http://localhost/users/page/2 7 | ``` 8 | 9 | ## Install 10 | 11 | Install this package via Composer: 12 | 13 | ``` 14 | composer require ctsoft/laravel-pretty-pagination 15 | ``` 16 | 17 | ## Usage 18 | 19 | To generate pretty URLs simply call the ```paginate()``` macro on your routes: 20 | 21 | ```php 22 | Route::get('/users', ...)->name('users')->paginate(); 23 | ``` 24 | 25 | If you wan't to change the prefix (default is ```page```): 26 | 27 | ```php 28 | Route::get('/users', ...)->name('users')->paginate('seite'); 29 | ``` 30 | 31 | Or if you don't want to use any prefix: 32 | 33 | ```php 34 | Route::get('/users', ...)->name('users')->paginate(null); 35 | ``` 36 | 37 | ## Notes 38 | 39 | - The route must have a name 40 | - The ```paginate()``` macro must be called as last 41 | 42 | ## Security 43 | 44 | If you discover any security related issues, please email [security@ctsoft.de](mailto:security@ctsoft.de) instead of using the issue tracker. 45 | 46 | ## License 47 | 48 | This package is open-sourced software licensed under the [MIT license](https://opensource.org/licenses/MIT). 49 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ctsoft/laravel-pretty-pagination", 3 | "description": "Pretty pagination URLs for Laravel.", 4 | "keywords": [ 5 | "laravel", 6 | "pagination", 7 | "pretty" 8 | ], 9 | "type": "library", 10 | "license": "MIT", 11 | "authors": [ 12 | { 13 | "name": "CTSoft", 14 | "email": "info@ctsoft.de" 15 | } 16 | ], 17 | "require": { 18 | "php": "^7.2|^8.0", 19 | "illuminate/container": "^6.0|^7.0|^8.0|^9.0|^10.0|^11.0", 20 | "illuminate/http": "^6.0|^7.0|^8.0|^9.0|^10.0|^11.0", 21 | "illuminate/pagination": "^6.0|^7.0|^8.0|^9.0|^10.0|^11.0", 22 | "illuminate/routing": "^6.0|^7.0|^8.0|^9.0|^10.0|^11.0", 23 | "illuminate/support": "^6.0|^7.0|^8.0|^9.0|^10.0|^11.0" 24 | }, 25 | "autoload": { 26 | "psr-4": { 27 | "CTSoft\\Laravel\\PrettyPagination\\": "src/" 28 | } 29 | }, 30 | "extra": { 31 | "laravel": { 32 | "providers": [ 33 | "CTSoft\\Laravel\\PrettyPagination\\Providers\\PrettyPaginationProvider" 34 | ] 35 | } 36 | }, 37 | "config": { 38 | "sort-packages": true 39 | }, 40 | "minimum-stability": "dev", 41 | "prefer-stable": true 42 | } 43 | -------------------------------------------------------------------------------- /src/Http/Middleware/SetPrettyPagination.php: -------------------------------------------------------------------------------- 1 | bind(BaseLengthAwarePaginator::class, LengthAwarePaginator::class); 27 | $container->bind(BasePaginator::class, Paginator::class); 28 | 29 | Paginator::currentPathResolver(function () use ($request) { 30 | return preg_replace('/\.page$/', '', $request->route()->getName()); 31 | }); 32 | 33 | Paginator::currentParametersResolver(function () use ($request) { 34 | return collect($request->route()->originalParameters())->forget('page')->all(); 35 | }); 36 | 37 | Paginator::currentPageResolver(function () use ($request) { 38 | return max((int)$request->route('page'), 1); 39 | }); 40 | 41 | return $next($request); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/Pagination/GeneratePrettyUrl.php: -------------------------------------------------------------------------------- 1 | getPageUrl(max($page, 1)); 27 | $url = $this->addQueryString($url); 28 | $url .= $this->buildFragment(); 29 | 30 | return $url; 31 | } 32 | 33 | /** 34 | * Get the URL for a page. 35 | * 36 | * @param int $page 37 | * @return string 38 | */ 39 | protected function getPageUrl(int $page): string 40 | { 41 | $route = $this->path(); 42 | $parameters = $this->parameters(); 43 | 44 | if ($page > 1) { 45 | $route .= '.page'; 46 | $parameters['page'] = $page; 47 | } 48 | 49 | return URL::route($route, $parameters); 50 | } 51 | 52 | /** 53 | * Add the query string to an URL. 54 | * 55 | * @param string $url 56 | * @return string 57 | */ 58 | protected function addQueryString(string $url): string 59 | { 60 | if (!$this->query) { 61 | return $url; 62 | } 63 | 64 | return sprintf('%s%s%s', 65 | $url, 66 | Str::contains($url, '?') ? '&' : '?', 67 | Arr::query($this->query) 68 | ); 69 | } 70 | 71 | /** 72 | * Get the parameters for paginator generated URLs. 73 | * 74 | * @return array 75 | */ 76 | protected function parameters(): array 77 | { 78 | if (!isset($this->parameters)) { 79 | $this->parameters = static::resolveCurrentParameters(); 80 | } 81 | 82 | return $this->parameters; 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /src/Pagination/LengthAwarePaginator.php: -------------------------------------------------------------------------------- 1 | middleware(SetPrettyPagination::class); 22 | 23 | $route = clone $this; 24 | 25 | $route->uri = sprintf('%s%s{page}', 26 | Str::finish($route->uri, '/'), 27 | $prefix ? Str::finish($prefix, '/') : '' 28 | ); 29 | 30 | $route->name('.page'); 31 | 32 | Router::getRoutes()->add($route); 33 | }; 34 | } 35 | } 36 | --------------------------------------------------------------------------------