From e4663130ad66d1618980e1c63ed00b05641a0bd2 Mon Sep 17 00:00:00 2001 From: Albert Mayor Date: Mon, 20 Oct 2025 15:25:22 +0200 Subject: [PATCH 1/2] Test finished --- composer.json | 3 +- controllers/SiteController.php | 69 +++++++++++++++++++++++++++++++++- package-lock.json | 6 +++ views/site/post.php | 22 +++++++++++ views/site/test1.php | 22 +++++++++++ web/css/site.css | 19 +++++++++- 6 files changed, 137 insertions(+), 4 deletions(-) create mode 100644 package-lock.json create mode 100644 views/site/post.php diff --git a/composer.json b/composer.json index f60a8f9..ed379d3 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": "*" }, "require-dev": { "yiisoft/yii2-debug": "~2.1.0", diff --git a/controllers/SiteController.php b/controllers/SiteController.php index 54a70f1..16b4ecd 100644 --- a/controllers/SiteController.php +++ b/controllers/SiteController.php @@ -4,6 +4,10 @@ use yii\web\Controller; +use yii\httpclient\Client; + +use yii\data\Pagination; + class SiteController extends Controller{ /** * {@inheritdoc} @@ -31,6 +35,69 @@ public function actionIndex(){ * @return string */ public function actionTest1(){ - return $this->render('test1'); + + /** + * For a simple retrieve case like this, we could use file_get_contents to fetch the data + * but since this is a Yii2 test, let's use Yii2's HTTP Client component to demonstrate best practices and + * proper usage of the framework, while avoiding possible allow_url_fopen exploits errors. + * + * Also let's try Yii2's pagination just to learn how it works, even if it's not required in the task. + */ + + $client = new Client(); + $response = $client->createRequest() + ->setMethod('GET') + ->setUrl("https://jsonplaceholder.typicode.com/posts") + ->send(); + if ($response->isOk) { + $data = $response->data; + } else { + $data = []; + } + + $count = count($data); + $pagination = new Pagination(['totalCount' => $count, 'pageSize' => 6]); + $articles = array_slice($data, $pagination->offset, $pagination->limit); + + return $this->render('test1', [ + 'data' => $articles, + 'pagination' => $pagination + ]); } + + /** + * Displays single post. + * + * @return string + */ + public function actionPost($postId = null, $page = 0){ + + /** + * Since we have some time left, let's add a simple post view page with a simple "post not found" handling. + * (you can test it by changing the postId parameter to a non-existing one in the URL) + */ + print_r($page); + + if(empty($postId)) { + return $this->redirect(['site/test1']); + } else { + $client = new Client(); + $response = $client->createRequest() + ->setMethod('GET') + ->setUrl("https://jsonplaceholder.typicode.com/posts/{$postId}") + ->send(); + + if ($response->isOk) { + $post = $response->data; + + } else { + $post = []; + } + } + + return $this->render('post', [ + 'post' => $post, + 'page' => $page + ]); + } } diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..7b33b46 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,6 @@ +{ + "name": "job-application-tests-php", + "lockfileVersion": 3, + "requires": true, + "packages": {} +} diff --git a/views/site/post.php b/views/site/post.php new file mode 100644 index 0000000..e9fcb8c --- /dev/null +++ b/views/site/post.php @@ -0,0 +1,22 @@ +title = 'Post — ooptimo'; +?> +
+
+ Back to posts list +
+ + Placeholder image +

+

+ +

Post not found.

+ +
+
+
diff --git a/views/site/test1.php b/views/site/test1.php index d08b3e3..c9ee782 100644 --- a/views/site/test1.php +++ b/views/site/test1.php @@ -2,6 +2,9 @@ /** @var yii\web\View $this */ +use yii\helpers\Url; +use yii\widgets\LinkPager; + $this->title = 'Test 1 — ooptimo'; ?>
@@ -29,5 +32,24 @@

Posts

+
+ +

No posts found.

+ + +
+ Placeholder image +

+ See post +
+ + + +
+ $pagination, + ]); ?> +
+
diff --git a/web/css/site.css b/web/css/site.css index 9b92ad0..9959d11 100644 --- a/web/css/site.css +++ b/web/css/site.css @@ -1,4 +1,4 @@ -main > .container { +main>.container { padding: 70px 15px 20px; } @@ -8,7 +8,7 @@ main > .container { height: 60px; } -.footer > .container { +.footer>.container { padding-right: 15px; padding-left: 15px; } @@ -28,3 +28,18 @@ main > .container { margin-top: 5px; color: #999; } + +a.post-title { + color: #333; + text-decoration: none; +} + +.pagination a, +.pagination span { + padding: 5px; + font-size: 20px; +} + +.pagination .active a { + font-weight: bold; +} \ No newline at end of file From 0c79c1b08cc11458fa38768a82441e95f0eb5b76 Mon Sep 17 00:00:00 2001 From: Albert Mayor Date: Mon, 20 Oct 2025 15:25:52 +0200 Subject: [PATCH 2/2] Removed a debug print from the site controller --- controllers/SiteController.php | 1 - 1 file changed, 1 deletion(-) diff --git a/controllers/SiteController.php b/controllers/SiteController.php index 16b4ecd..4bde1ce 100644 --- a/controllers/SiteController.php +++ b/controllers/SiteController.php @@ -76,7 +76,6 @@ public function actionPost($postId = null, $page = 0){ * Since we have some time left, let's add a simple post view page with a simple "post not found" handling. * (you can test it by changing the postId parameter to a non-existing one in the URL) */ - print_r($page); if(empty($postId)) { return $this->redirect(['site/test1']);