Last active
August 24, 2021 15:14
-
-
Save jhaoda/115b9866cc08e13f2d6e to your computer and use it in GitHub Desktop.
ЧПУ-пагинация
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// ... | |
'providers' => [ | |
//'Illuminate\Pagination\PaginationServiceProvider', | |
'App\Providers\PrettyPaginationServiceProvider', | |
], | |
// ... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use App\Services\PrettyPaginator; | |
use Illuminate\Pagination\Paginator; | |
use Illuminate\Database\Eloquent\Builder; | |
use Illuminate\Contracts\Pagination\Paginator as PaginatorContract; | |
/** | |
* ЧПУ-пагинация. | |
* | |
* @param Builder $items | |
* @param string $orderBy | |
* @param string $sort | |
* | |
* @return PaginatorContract|PrettyPaginator | |
*/ | |
public static function paginate($items, $orderBy = 'created_at', $sort = 'DECS') | |
{ | |
$page = Paginator::resolveCurrentPage(); | |
$perPage = $this->getPerPage(); | |
$rawQuery = $items->getQuery()->orderBy($orderBy, $sort); | |
$rawQuery->forPage($page, $perPage); | |
return new PrettyPaginator( | |
$items->get(['*']), $rawQuery->getCountForPagination(), $perPage, $page, | |
[ | |
'path' => Paginator::resolveCurrentPath(), | |
] | |
); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Providers; | |
use Illuminate\Pagination\Paginator; | |
use Illuminate\Support\ServiceProvider; | |
class PrettyPaginationServiceProvider extends ServiceProvider | |
{ | |
public function register() | |
{ | |
Paginator::currentPathResolver(function() | |
{ | |
return $this->app['request']->url(); | |
}); | |
Paginator::currentPageResolver(function() | |
{ | |
return $this->app['request']->route()->parameter('page'); | |
}); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Services; | |
use Illuminate\Pagination\LengthAwarePaginator; | |
class PrettyPaginator extends LengthAwarePaginator | |
{ | |
/** | |
* {@inheritdoc} | |
*/ | |
public function url($page) | |
{ | |
$path = explode('/', rtrim($this->path, '/')); | |
if ($this->currentPage > 1) { | |
if ($page == 1) { | |
array_pop($path); | |
array_pop($path); | |
} else { | |
$path[count($path) - 1] = $page; | |
} | |
} else { | |
$path[] = 'page'; | |
$path[] = $page; | |
} | |
return join('/', $path); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
Route::get('posts/page/{page}', 'PostsController@index'); | |
Route::get('users/page/{page}', 'UsersController@index'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment