From 35c68f45ae8e1fdc707cbca9b81a248cb3feb354 Mon Sep 17 00:00:00 2001 From: Carlos Turmo Date: Tue, 21 Oct 2025 12:02:21 +0200 Subject: [PATCH 1/2] Docker compose file fixed --- docker-compose.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 86be3bd..276c39e 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,4 +1,3 @@ -version: '2' services: php: image: yiisoftware/yii2-php:7.4-apache @@ -6,4 +5,4 @@ services: - ~/.composer-docker/cache:/root/.composer/cache:delegated - ./:/app:delegated ports: - - '8000:80' + - '8080:80' From 29f7da6adc2f4f11387ba18e5c3b19b95838c384 Mon Sep 17 00:00:00 2001 From: Carlos Turmo Date: Tue, 21 Oct 2025 18:34:34 +0200 Subject: [PATCH 2/2] feat: backend PHP application test - retrieve API data and display with error handling for Test 1 - pagination included --- composer.json | 3 +- controllers/SiteController.php | 28 ++++++++++- models/Post.php | 90 ++++++++++++++++++++++++++++++++++ views/layouts/main.php | 2 +- views/site/test1.php | 46 +++++++++++++++-- web/css/site.css | 43 ++++++++++++++++ 6 files changed, 205 insertions(+), 7 deletions(-) create mode 100644 models/Post.php diff --git a/composer.json b/composer.json index f60a8f9..c267644 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,8 @@ "php": ">=7.4.0", "yiisoft/yii2": "~2.0.45", "yiisoft/yii2-bootstrap5": "~2.0.2", - "yiisoft/yii2-symfonymailer": "~2.0.3" + "yiisoft/yii2-symfonymailer": "~2.0.3", + "yiisoft/yii2-httpclient": "^2.0" }, "require-dev": { "yiisoft/yii2-debug": "~2.1.0", diff --git a/controllers/SiteController.php b/controllers/SiteController.php index 54a70f1..0121dde 100644 --- a/controllers/SiteController.php +++ b/controllers/SiteController.php @@ -2,7 +2,10 @@ namespace app\controllers; +use Yii; use yii\web\Controller; +use app\models\Post; +use yii\data\Pagination; class SiteController extends Controller{ /** @@ -31,6 +34,27 @@ public function actionIndex(){ * @return string */ public function actionTest1(){ - return $this->render('test1'); + + // Number of posts per page + $pageSize = 9; + + // Get current page number from the request + $page = Yii::$app->request->get('page', 1); + + // Fetch posts from the Post model including pagination + $posts = Post::fetchAll($page, $pageSize); + + // Set up pagination + $pagination = new Pagination([ + 'totalCount' => $posts['totalCount'], + 'pageSize' => $pageSize, + 'page' => $page - 1, // Adjust for zero-based index + ]); + + // Render the view with posts and pagination + return $this->render('test1', [ + 'posts' => $posts['data'], + 'pagination' => $pagination, + ]); } -} +} \ No newline at end of file diff --git a/models/Post.php b/models/Post.php new file mode 100644 index 0000000..d77ac4d --- /dev/null +++ b/models/Post.php @@ -0,0 +1,90 @@ + 'https://jsonplaceholder.typicode.com/']); + + // Try to send the request and handle possible errors + try { + + // Send GET request to the /posts endpoint using Yii2 HttpClient with params for pagination + // Documentation: https://github.com/typicode/json-server#paginate + // We use _limit instead of _per_page because JSONPlaceholder is using old json-server version + $response = $client->get('posts', [ + '_page' => $page, + '_limit' => $limit, + ])->send(); + + // Check if the response is successful + if ($response->isOk) { + $posts = $response->data; + + // Total count of posts from the headers + $totalCount = (int) $response->headers->get('X-Total-Count', count($posts)); + + // Return posts data and total count + return [ + 'data' => $posts, + 'totalCount' => $totalCount, + ]; + } + + /* + Handle different error status codes from the API related with getting posts. + Taking into account it's not necessary credentials for this API. + */ + switch ($response->statusCode) { + case 400: + $message = 'Bad Request (400).'; + break; + case 404: + $message = 'Resource not found (404).'; + break; + case 500: + $message = 'Internal server error (500).'; + break; + case 503: + $message = 'Service unavailable (503). Please try again later.'; + break; + default: + $message = 'Unknown error. Code: ' . $response->statusCode; + break; + } + + // Yii2 log to capture possible API errors + Yii::warning("Error API ({$response->statusCode}): {$message}", __METHOD__); + + return [ + 'error' => true, + 'message' => $message, + ]; + + + } catch (\Exception $e) { + + // Yii2 log to capture connection errors + Yii::error("Failed to connect: " . $e->getMessage(), __METHOD__); + return [ + 'error' => true, + 'message' => 'Sorry, there was an error connecting to the external {JSON} Placeholder API.', + ]; + } + + } +} \ No newline at end of file diff --git a/views/layouts/main.php b/views/layouts/main.php index b0baf83..896ed12 100644 --- a/views/layouts/main.php +++ b/views/layouts/main.php @@ -46,7 +46,7 @@ -