-
Notifications
You must be signed in to change notification settings - Fork 6
Job application tests PHP #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -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 = []; | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
Comment on lines
+89
to
+94
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The error handling in Consider adding a check before rendering or redirecting to an error page:
Suggested change
|
||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| return $this->render('post', [ | ||||||||||||||||||||||||||||||
| 'post' => $post, | ||||||||||||||||||||||||||||||
| 'page' => $page | ||||||||||||||||||||||||||||||
| ]); | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| <?php | ||
|
|
||
| /** @var yii\web\View $this */ | ||
|
|
||
| use yii\helpers\Url; | ||
|
|
||
| $this->title = 'Post — ooptimo'; | ||
| ?> | ||
| <div class="site-post"> | ||
| <div class="body-content"> | ||
| <a href="<?=Url::to(['site/test1', 'page' => $page])?>" class="btn btn-primary btn-sm mb-3">Back to posts list</a> | ||
| <div class="row mt-3"> | ||
| <?php if ($post): ?> | ||
| <img src="https://placehold.co/800x400" alt="Placeholder image" class="img-fluid mb-4"> | ||
| <h2><?= htmlspecialchars($post['title']) ?></h2> | ||
| <p><?= htmlspecialchars($post['body']) ?></p> | ||
| <?php else: ?> | ||
| <p>Post not found.</p> | ||
| <?php endif; ?> | ||
| </div> | ||
| </div> | ||
| </div> |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -2,6 +2,9 @@ | |||||
|
|
||||||
| /** @var yii\web\View $this */ | ||||||
|
|
||||||
| use yii\helpers\Url; | ||||||
| use yii\widgets\LinkPager; | ||||||
|
|
||||||
| $this->title = 'Test 1 — ooptimo'; | ||||||
| ?> | ||||||
| <div class="site-test1"> | ||||||
|
|
@@ -29,5 +32,24 @@ | |||||
| <div class="row mt-4"> | ||||||
| <h2>Posts</h2> | ||||||
| </div> | ||||||
| <div class="row mt-3"> | ||||||
| <?php if(empty($data)): ?> | ||||||
| <p>No posts found.</p> | ||||||
| <?php else: ?> | ||||||
| <?php foreach($data as $post): ?> | ||||||
| <div class="col-md-4 post-item mb-3"> | ||||||
| <a href="<?=Url::to(['site/post', 'postId' => $post['id'], 'page' => isset($_GET['page']) ? $_GET['page'] : 1])?>"><img src="https://placehold.co/600x400" alt="Placeholder image" class="img-fluid mb-2"></a> | ||||||
| <a href="<?=Url::to(['site/post', 'postId' => $post['id'], 'page' => isset($_GET['page']) ? $_GET['page'] : 1])?>" class="post-title"><h4><?= htmlspecialchars($post['title']) ?></h4></a> | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of directly accessing
Suggested change
|
||||||
| <a href="<?=Url::to(['site/post', 'postId' => $post['id'], 'page' => isset($_GET['page']) ? $_GET['page'] : 1])?>" class="btn btn-primary btn-sm mb-3">See post</a> | ||||||
| </div> | ||||||
| <?php endforeach; ?> | ||||||
| <?php endif; ?> | ||||||
|
|
||||||
| <div class="mt-4 d-flex justify-content-center"> | ||||||
| <?php echo LinkPager::widget([ | ||||||
| 'pagination' => $pagination, | ||||||
| ]); ?> | ||||||
| </div> | ||||||
| </div> | ||||||
| </div> | ||||||
| </div> | ||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a potential security issue with directly inserting the
$postIdparameter into the URL string. While jsonplaceholder.typicode.com might handle this safely, it's a good practice to validate or sanitize URL parameters before using them in requests.