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..4bde1ce 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,68 @@ 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) + */ + + 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'; +?> +
= htmlspecialchars($post['body']) ?>
+ +Post not found.
+ +No posts found.
+ + + + + + +