Add symfony serializer with composer require serializer command. Add api endpoint. Use logger service in new controller. Add some phony model.

This commit is contained in:
Krzysztof Płaczek
2024-10-17 21:00:58 +02:00
parent d4aa0dde32
commit 303d742659
4 changed files with 742 additions and 1 deletions

View File

@@ -0,0 +1,28 @@
<?php
namespace App\Controller;
use App\Model\Starship;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
class StarshipApiController extends AbstractController
{
#[Route('/api/starships', name: 'starships')]
public function getCollection(LoggerInterface $logger): Response
{
$logger->info('Starships get collection');
$starships = [
new Starship(
1, 'Starship 01', 'Heavy Starship', 'Human Captain', 'damaged',
),
new Starship(
2, 'Starship 02', 'Light Starship', 'Robot Captain', 'new',
),
];
return $this->json($starships);
}
}

40
src/Model/Starship.php Normal file
View File

@@ -0,0 +1,40 @@
<?php
namespace App\Model;
class Starship
{
public function __construct(
private string $id,
private string $name,
private string $class,
private string $captain,
private string $status,
) {
}
public function getCaptain(): string
{
return $this->captain;
}
public function getClass(): string
{
return $this->class;
}
public function getId(): string
{
return $this->id;
}
public function getName(): string
{
return $this->name;
}
public function getStatus(): string
{
return $this->status;
}
}