Implement API consumption with pagination for posts#9
Implement API consumption with pagination for posts#9frangirona86 wants to merge 1 commit intoooptimo:mainfrom
Conversation
There was a problem hiding this comment.
Other comments (3)
- controllers/SiteController.php (73-85) The implementation fetches all posts from the API and then applies pagination locally. For real-world APIs with large datasets, this approach is inefficient. Consider using the API's built-in pagination parameters (if available) to request only the needed subset of data.
-
views/site/posts.php (3-8)
The variables `$totalPages` and `$currentPage` are used in the pagination section but aren't documented in the PHPDoc at the top of the file. Consider adding them to the documentation for better code clarity:
/** @var yii\web\View $this */ /** @var array $data */ /** @var string|null $error */ /** @var int $totalPages */ /** @var int $currentPage */ use yii\helpers\Url; -
controllers/SiteController.php (38-40)
The method docblock incorrectly states the return type is `array $posts`, but the method actually returns the result of `$this->render()` which is a string/response.
* Displays Test Posts Francisco Girona * @return string */
💡 To request another review, post a new comment with "/windsurf-review".
| <?php foreach($data as $post): ?> | ||
| <article class="post"> | ||
| <h2 class="post-title"><?= Html::encode($post['title']) ?></h2> | ||
| <p class="post-body"><?= Html::encode($post['body']) ?></p> | ||
| </article> | ||
| <?php endforeach; ?> |
There was a problem hiding this comment.
There's no check to ensure $data is an array before the foreach loop. If $data is null or not an array, this could cause a PHP error. Consider adding a check or using empty() instead:
| <?php foreach($data as $post): ?> | |
| <article class="post"> | |
| <h2 class="post-title"><?= Html::encode($post['title']) ?></h2> | |
| <p class="post-body"><?= Html::encode($post['body']) ?></p> | |
| </article> | |
| <?php endforeach; ?> | |
| <?php if(!empty($data)): ?> | |
| <?php foreach($data as $post): ?> | |
| <article class="post"> | |
| <h2 class="post-title"><?= Html::encode($post['title']) ?></h2> | |
| <p class="post-body"><?= Html::encode($post['body']) ?></p> | |
| </article> | |
| <?php endforeach; ?> | |
| <?php endif; ?> |
| $totalPosts = count($posts); | ||
| $postsPerPage = 10; | ||
| $totalPages = ceil($totalPosts / $postsPerPage); | ||
| $currentPage = isset($_GET['page']) ? (int) $_GET['page'] : 1; |
There was a problem hiding this comment.
The page parameter from $_GET should be validated to ensure it's numeric before casting to integer. Consider using filter_input() or checking with is_numeric() first.
| $currentPage = isset($_GET['page']) ? (int) $_GET['page'] : 1; | |
| $currentPage = isset($_GET['page']) && is_numeric($_GET['page']) ? (int) $_GET['page'] : 1; |
| ], | ||
| ]); | ||
|
|
||
| $raw = @file_get_contents($url, false, $context); |
There was a problem hiding this comment.
Avoid using the @ error suppression operator with file_get_contents(). It's better to handle errors explicitly using try/catch or by checking error conditions with error_get_last() after the operation.
What I implemented
I completed PHP Test by consuming the JSONPlaceholder API and displaying the posts on a paginated list.
Main features:
Technical choices:
I used
file_get_contents()with a custom stream context instead of cURL because it's simpler for this use case. Added a 5-second timeout and SSL verification for security.For the view, I used
Html::encode()to prevent XSS attacks and structured the posts with<article>tags for better semantics.The CSS is minimal, focusing on clean post presentation and functional pagination.
Let me know if you need any clarifications or changes!