Conversation
…th error handling for Test 1 - pagination included
There was a problem hiding this comment.
Other comments (5)
- controllers/SiteController.php (49-49) There's no validation to ensure that `$posts` contains the expected 'totalCount' key before using it. Consider adding a check to ensure the data structure is valid before creating the Pagination object.
-
web/css/site.css (69-73)
The nested CSS syntax used for the footer selector (`#footer{ .text-muted{ ... } }`) is not valid standard CSS and requires a preprocessor like Sass. If this project doesn't use a CSS preprocessor, this will cause syntax errors. Consider using standard CSS syntax instead:
#footer .text-muted{ color: var(--bs-white)!important; } - controllers/SiteController.php (45-45) The `Post::fetchAll()` call doesn't have any error handling. Consider wrapping this in a try-catch block to gracefully handle API failures and provide appropriate feedback to the user.
- models/Post.php (24-25) Consider removing the unnecessary empty line after the try statement to maintain consistent code style.
- models/Post.php (78-79) Consider removing the unnecessary empty line before the catch statement to maintain consistent code style.
💡 To request another review, post a new comment with "/windsurf-review".
| // Yii2 log to capture possible API errors | ||
| Yii::warning("Error API ({$response->statusCode}): {$message}", __METHOD__); |
There was a problem hiding this comment.
The Yii class is being used for logging but hasn't been imported. Please add use Yii; at the top of the file with the other imports.
| /** @var yii\web\View $this */ | ||
|
|
||
| /* Yii2 pagination widget */ | ||
| use yii\widgets\LinkPager; |
There was a problem hiding this comment.
There's a namespace inconsistency with the LinkPager widget. At the top of the file, you import yii\widgets\LinkPager, but later use \yii\bootstrap5\LinkPager. You should update the import statement to match the actual implementation.
| <div class="col-md-12"> | ||
| <div class="alert alert-danger" role="alert"> | ||
| <!-- Display the error message --> | ||
| <?php echo $posts['message']; ?> |
There was a problem hiding this comment.
The error message is being output directly without HTML escaping, which could lead to XSS vulnerabilities if the error message contains HTML tags or scripts.
| <?php echo $posts['message']; ?> | |
| <!-- Display the error message --> | |
| <?php echo htmlspecialchars($posts['message']); ?> |
Backend PHP application test - Retrieve API data and display with error handling - Pagination included
Primer de tot he modificat l'arxiu docker-compose.yml (l'atribut version està obsolet) per poder executar el projecte amb Docker. També he canviat el port a 8080 per unificar-ho amb les instruccions de l'exercici.
Respecte a paquets nous, he afegit yii2-httpclient per gestionar les crides a l'API.
A l'hora d'afegir la paginació m'he trobat que JSONPlaceholder utiliza una versió antiga de json-server, per tant els paràmetres per fer la crida dins el model Post no corresponen amb la documentació (paràmetre _limit en comptes de _per_page).
He modificat mínimament el CSS per adaptar colors.
Per executar i accedir a Docker:
docker compose up -d
docker compose exec php bash