Code Monkey home page Code Monkey logo

lpcanvas's People

Watchers

 avatar  avatar

lpcanvas's Issues

Serialization

Pré requis

Dépendances

Jouer la commande suivante à la racine de votre projet
composer require symfony/serializer-pack

Objet à sérialiser App\Entity\User

Composition de l'objet

  • $id -> int
  • $firstName -> string
  • $lastName -> string
  • $birthDate -> \DateTime
  • $updatedAt -> \DateTime

Controller App\Controller\UserController

  • Doit etendre la classe Symfony\Bundle\FrameworkBundle\Controller\AbstractController

/user/{lastName}/{firstName}

  • lastName -> string
  • firstName -> string

Doit construire un objet User qui contient les données reçu par le controller

Renvoie le json avec $this->json($data);

Regarder le retour json

Serialization App\Services\Normalizer\UserSerializer

  • Doit implémenter Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface

La serialisation doit renvoyer les données suivantes :

{
  "id": 4,
  "firstName": "Bob",
  "lastName": "Bobby",
  "birthDate": "2000-05-04"
}

Controller App\Controller\UserController

  • Doit etendre la classe Symfony\Bundle\FrameworkBundle\Controller\AbstractController

/user/list

Renvoie une liste de User

Doc : https://symfony.com/doc/current/serializer.html

Créer votre premier controller

Etape 1

  • Créer un fichier HelloController dans le dossier src/HelloController
  • Créer une fonction helloAction qui renvoie une JsonResponse. Le contenu importe peu
  • Configurer l'url dans le fichier routes.yaml

Etape 2 : Parametres de routes

  • Créer une fonction numberAction
    • Paramètre : $number
    • Retour: JsonResponse contenant $number

Etape 3 : Verouiller le type de paramètre

  • Créer une fonction numberAction
    • Paramètre : $number. C'est obligatoirement un nombre
    • Retour: JsonResponse contenant $number

Etape 4 : Verouiller le type de méthode

  • Créer une fonction putAction
    • Retour: JsonResponse contenant ce que vous voulez
    • Cette route ne doit répondre qu'à la méthode d'appel PUT

Doc : https://symfony.com/doc/current/controller.html

Controller - Suite

Configuration

Fichier plane.yaml

  • Créer le fichier plane.yaml dans le dossier config/routes

Liste des routes

Liste des avions

  • /
  • GET

Ajout d'un avion

  • /
  • PUT

Detail d'un avion

  • /{id}
  • GET
  • id -> int

Modification d'un avion

  • /{id}
  • PATCH
  • id -> int

Suppression d'un avion

  • /{id}
  • DELETE
  • id -> int

Fichier config/routes.yaml

  • Dans le fichier config/routes.yaml, importer le fichier plane.yaml. Voici une exemple
boat:
  resource: './routes/boat.yaml'
  prefix: /boat

Controller

Dans votre controller PlaneController, créer les actions correspondantes à toutes les routes citées dans la première partie

Ces routes doivent renvoyer des codes HTTP logiques (Pas que des 200)

Comment tester

CURL

Exemples par méthode

  • curl -i -X GET http://localhost:8000/plane/
  • curl -i -X PUT -d name='Le Gros Avion' model='A380' code='QUANTUM' manufacturer='BOB' http://localhost:8000/plane/5
  • curl -i -X PATCH -d name='Le Pas Si Gros Avion' http://localhost:8000/plane/5
  • curl -i -X DELETE http://localhost:8000/plane/5

Doctrine

Pré requis

Dépendances

Jouer la commande suivante à la racine de votre projet
composer require symfony/orm-pack

Schéma de base

airbuse

Script

-- MySQL dump 10.13  Distrib 5.7.28, for Linux (x86_64)
--
-- Host: 127.0.0.1    Database: example
-- ------------------------------------------------------
-- Server version       8.0.18

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;

--
-- Table structure for table `airport`
--

DROP TABLE IF EXISTS `airport`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `airport` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `code` varchar(5) DEFAULT NULL,
  `name` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `airport`
--

LOCK TABLES `airport` WRITE;
/*!40000 ALTER TABLE `airport` DISABLE KEYS */;
/*!40000 ALTER TABLE `airport` ENABLE KEYS */;
UNLOCK TABLES;

--
-- Table structure for table `flight`
--

DROP TABLE IF EXISTS `flight`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `flight` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `departure_airport` int(11) DEFAULT NULL,
  `arrival_airport` int(11) DEFAULT NULL,
  `departure_datetime` datetime DEFAULT NULL,
  `arrival_departure` datetime DEFAULT NULL,
  `main_pilot` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `flight_arrival_fk` (`arrival_airport`),
  KEY `flight_departure_fk` (`departure_airport`),
  KEY `flight_pilot_fk` (`main_pilot`),
  CONSTRAINT `flight_arrival_fk` FOREIGN KEY (`arrival_airport`) REFERENCES `airport` (`id`),
  CONSTRAINT `flight_departure_fk` FOREIGN KEY (`departure_airport`) REFERENCES `airport` (`id`),
  CONSTRAINT `flight_pilot_fk` FOREIGN KEY (`main_pilot`) REFERENCES `pilot` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `flight`
--

LOCK TABLES `flight` WRITE;
/*!40000 ALTER TABLE `flight` DISABLE KEYS */;
/*!40000 ALTER TABLE `flight` ENABLE KEYS */;
UNLOCK TABLES;

--
-- Table structure for table `pilot`
--

DROP TABLE IF EXISTS `pilot`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `pilot` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `first_name` varchar(100) DEFAULT NULL,
  `last_name` varchar(100) DEFAULT NULL,
  `birth_date` date DEFAULT NULL,
  `hiring_date` datetime DEFAULT NULL,
  `updated_at` datetime DEFAULT NULL,
  `created_at` datetime DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `pilot`
--

LOCK TABLES `pilot` WRITE;
/*!40000 ALTER TABLE `pilot` DISABLE KEYS */;
/*!40000 ALTER TABLE `pilot` ENABLE KEYS */;
UNLOCK TABLES;

--
-- Table structure for table `plane`
--

DROP TABLE IF EXISTS `plane`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `plane` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) DEFAULT NULL,
  `model` varchar(25) DEFAULT NULL,
  `code` varchar(50) DEFAULT NULL,
  `manufacturer` varchar(5) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `plane`
--

LOCK TABLES `plane` WRITE;
/*!40000 ALTER TABLE `plane` DISABLE KEYS */;
/*!40000 ALTER TABLE `plane` ENABLE KEYS */;
UNLOCK TABLES;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;

/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

-- Dump completed on 2019-12-11  6:51:08

Créer les objets correspondant dans le namespace App\Entity

Créer les sérializers associés aux objets

Créer les routes correspondantes pour récuperer la liste des objets et le detail d'un objet

AirportController

  • GET /airport
  • GET /airport/{id}
  • DELETE /airport/{id}

FlightController

  • GET /flight
  • GET /flight/{id}
  • DELETE /flight/{id}

PilotController

  • GET /pilot
  • GET /pilot/{id}
  • DELETE /pilot/{id}

PlaneController

  • GET /plane
  • GET /plane/{id}
  • DELETE /plane/{id}

Doc: https://symfony.com/doc/current/doctrine.html

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.