- Популярные видео
- Авто
- Видео-блоги
- ДТП, аварии
- Для маленьких
- Еда, напитки
- Животные
- Закон и право
- Знаменитости
- Игры
- Искусство
- Комедии
- Красота, мода
- Кулинария, рецепты
- Люди
- Мото
- Музыка
- Мультфильмы
- Наука, технологии
- Новости
- Образование
- Политика
- Праздники
- Приколы
- Природа
- Происшествия
- Путешествия
- Развлечения
- Ржач
- Семья
- Сериалы
- Спорт
- Стиль жизни
- ТВ передачи
- Танцы
- Технологии
- Товары
- Ужасы
- Фильмы
- Шоу-бизнес
- Юмор
[Symfony Tutorials] : How to create a rest API using symfony
# 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 https://localhost/api/v1/products.json and https://localhost/api/v1/products.jsonld
11. Check https://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 create a rest API using symfony канала Hugo Resende
- [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 https://localhost/api/v1/products.json and https://localhost/api/v1/products.jsonld
11. Check https://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 create a rest API using symfony канала Hugo Resende
Комментарии отсутствуют
Информация о видео
4 октября 2024 г. 20:59:57
00:10:35
Другие видео канала

![[Symfony Tutorials] : How to Implement Authentication and Authorization with Symfony](https://i.ytimg.com/vi/H1EBFyfWmPc/default.jpg)
![[Symfony Tutorials] : How to use mysql DB with dunglas/symfony-docker](https://i.ytimg.com/vi/j6T32WGyz_o/default.jpg)
![[Symfony Tutorials] : How to make Entity CRUD Controller Twig views](https://i.ytimg.com/vi/N0MXry_rAR4/default.jpg)
![[Symfony Tutorials] : How to consume an API using symfony ?](https://i.ytimg.com/vi/vkiRuwnx-yA/default.jpg)
![[Symfony Tutorials] : How to run symfony project with dunglas/symfony-docker](https://i.ytimg.com/vi/TMZSeEujRgM/default.jpg)
![[Symfony Tutorials] : How to use JWT token in API](https://i.ytimg.com/vi/jdjG4Lp2fls/default.jpg)