Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

when the page number > total pages ,it will cause 500 error #629

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions blog/src/Blog/BlogController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,18 @@
use Yiisoft\Router\HydratorAttribute\RouteArgument;
use Yiisoft\User\CurrentUser;
use Yiisoft\Yii\View\Renderer\ViewRenderer;
use App\Service\Web

final class BlogController
{
private const POSTS_PER_PAGE = 3;
private const POPULAR_TAGS_COUNT = 10;
private const ARCHIVE_MONTHS_COUNT = 12;
private const ARCHIVE_MONTHS_COUNT = 12;


private ViewRenderer $viewRenderer;

public function __construct(ViewRenderer $viewRenderer)
public function __construct(
private ViewRenderer $viewRenderer,
private readonly WebControllerService $webService)
{
$this->viewRenderer = $viewRenderer->withControllerName('blog');
}
Expand All @@ -31,12 +33,18 @@ public function index(
TagRepository $tagRepository,
ArchiveRepository $archiveRepo,
CurrentUser $currentUser,
#[RouteArgument('page')] $pageNum = 1,
#[RouteArgument('page')] int $pageNum = 1,
): Response {
$dataReader = $postRepository->findAllPreloaded();
$paginator = (new OffsetPaginator($dataReader))
->withPageSize(self::POSTS_PER_PAGE)
->withCurrentPage((int)$pageNum);

$offsetPaginator = (new OffsetPaginator($dataReader))
->withPageSize(self::PRODUCTS_PER_PAGE);
$totalPage = $offsetPaginator->getTotalPages();
if ($pageNum > $totalPage || $pageNum < 1) {
return $this->webService->getNotFoundResponse();
}
$paginator = $offsetPaginator
->withCurrentPage($pageNum);

$data = [
'paginator' => $paginator,
Expand Down
Loading