Bicycle/App/Controller/IndexController.php
Egor Isaev b65603b4d0 dev
2026-06-23 10:44:18 +03:00

58 lines
1.6 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/**
* @package Bicycle
* @author Egor Isaev
* @description IndexController.php
* @copyright (c) 03/06/2026
*/
namespace App\Controller;
use System\Classes\Controller;
use System\Classes\HTTP\Client\Curl;
use System\Classes\HTTP\Client\Request as HttpRequest;
/**
* Демонстрационный контроллер главной страницы.
*/
class IndexController extends Controller
{
/**
* Главная страница: GET-запрос к внешнему API, вывод todo в шаблон.
*
* @return string
* @throws \System\Classes\MyException
*/
public function indexAction(): string
{
// GET-запрос
$response = (new Curl())->execute(
HttpRequest::factory('https://jsonplaceholder.typicode.com/todos/1')
);
$todo = $response->isSuccess() ? $response->json() : [];
return $this->render('index', ['todo' => $todo]);
}
/**
* Демонстрация POST-запроса с JSON-телом к внешнему API.
*
* @return string
* @throws \System\Classes\MyException
*/
public function postAction(): string
{
// POST с JSON-телом
$response = (new Curl())->execute(
HttpRequest::factory('https://jsonplaceholder.typicode.com/posts')
->method('POST')
->json(['title' => 'Bicycle', 'body' => 'test', 'userId' => 1])
);
$created = $response->isSuccess() ? $response->json() : [];
return $this->render('index', ['created' => $created]);
}
}