[Symfony Tutorials] : How to create a rest API using symfony

  Рет қаралды 30

Hugo Resende

Hugo Resende

Күн бұрын

TUTORIAL 6
[Symfony Tutorials] : How to create a restAPI using symfony ?
---- to run project : docker compose up --pull always -d --wait OR use vscode extension Docker
REST API (Representational State Transfer API)
1. composer require api
2. php bin/console make:entity Product --api-resource
-(name (string) description (text) price (float) createdAt (datetime_imm null)
3. php bin/console make:migration && php bin/console doctrine:migrations:migrate
4. php bin/console debug:router
5. Change in config/routes/api_plataform.yaml
prefix : /api/v1
6. Change in config/packages/api_plataform.yaml
formats:
jsonld: ['application/ld+json']
json: ['application/json'] # Add support for JSON
7. On POSTMAN create new product, check products (header with application/ld+json or application/ld+json)
8. Test again create a product and check on browser too
9. On postman, edit a product using patch request with header : Content-Type : application/merge-patch+json
10. Check diffs between localhost/api/v1/products.json and localhost/api/v1/products.jsonld
11. Check localhost/api/v1 for API auto generated documentation
12. Add this to model to handle errors on missing fields :
(use Symfony\Component\Validator\Constraints as Assert;)
#[Assert\NotNull(message: "Price cannot be null.")]
#[Assert\Positive(message: "Price must be a positive number.")]
13. That's it, your simple restAPI is working
Thank you for watching....
check my source code at my github repo, hugoresende27.....
By Hugo Resende !!!!!!
1. First let's make entity Product
php bin/console make:entity Product
php bin/console make:migration && php bin/console doctrine:migrations:migrate
php bin/console make:controller API\\ProductAPIController
2. Change API controller index function :
#[Route('api/v1/product', name: 'api_product')]
public function index(ProductRepository $productRepository): JsonResponse
{
$products = $productRepository- findAll();
return $this- json($products);
}
8. Make a ProductAPIController, delete the generated template (we don't need it atm) and index function on controller
9. Create update function :
#[Route('/api/products/{id}', name: 'api_product_update')]
public function update(Product $product, Request $request, EntityManagerInterface $entityManager, SerializerInterface $serializer): Response
{
$requestData = $request- getContent();
$updatedProduct = $serializer- deserialize($requestData, Product::class, 'json');
$product- setName($updatedProduct- getName());
$product- setPrice($updatedProduct- getPrice());
$entityManager- flush();
return new Response('Product updated!', 200);
}

Пікірлер
[Symfony Tutorials] : How to consume an API using symfony ?
14:31
Epic Reflex Game vs MrBeast Crew 🙈😱
00:32
Celine Dept
Рет қаралды 34 МЛН
Bike Vs Tricycle Fast Challenge
00:43
Russo
Рет қаралды 112 МЛН
Officer Rabbit is so bad. He made Luffy deaf. #funny #supersiblings #comedy
00:18
Funny superhero siblings
Рет қаралды 18 МЛН
Kluster Duo #настольныеигры #boardgames #игры #games #настолки #настольные_игры
00:47
The AI-Powered VS Code Killer? Checking Out Cursor and AI Coding
26:33
Milan Jovanović
Рет қаралды 18 М.
Experiment No. 8  Maven Build Life Cycle through DB Project
38:57
Маниакальная фаза бюджетного планирования. Статус S08E05
1:06:46
Building Microsoft Sentinel integrations  -  Part 2: Creating data connectors
1:11:16
Microsoft Security Community
Рет қаралды 710
Build Your Own Custom Auth System with Fortify
20:14
Laravel
Рет қаралды 6 М.
FORM LINK NAVIGATION - HTML5
22:44
Bill Yeeplah
Рет қаралды 666
AstroAI Lunch Talk - October 7, 2024 - Bonny Wang
38:17
Epic Reflex Game vs MrBeast Crew 🙈😱
00:32
Celine Dept
Рет қаралды 34 МЛН