Api-platform

  • Tutorial
image
With the approach of Web 3.0, API first and Linked Data (or something like the Sematic Web) approaches are increasingly being used. In this regard, readers are invited to familiarize themselves with the framework for conveniently creating APIs based on schemes assembled on schema.org with generating answers in the form of JSON-LD .

This is a shortened version of the Api-platform tutorial


  1. Installing a project using Composer
    composer create-project api-platform/api-platform blog-api
    

  2. Remove the default demo
    1. We clear the files app / config / schema.yml and app / config / services.yml
    2. delete all files in src / AppBundle / Entity

  3. Creating a data model
    1. Api-platform contains a schema.org dictionary model generator
    2. We go to the site and find a scheme that describes what we need. In this case, schema.org/BlogPosting
    3. Now you need to create the configuration file app / config / schema.yml. In it, we indicate in which namespace to create types, what types to create and which fields we need (because the Schema.org website (http://schema.org/) contains the most complete entity diagrams and usually we need only a subset of the described fields) . In addition, it is possible to describe the relationships between entities.

      # app/config/schema.yml
      annotationGenerators:
          - ApiPlatform\SchemaGenerator\AnnotationGenerator\PhpDocAnnotationGenerator
          - ApiPlatform\SchemaGenerator\AnnotationGenerator\DoctrineOrmAnnotationGenerator
          - ApiPlatform\SchemaGenerator\AnnotationGenerator\ConstraintAnnotationGenerator
          - ApiPlatform\SchemaGenerator\AnnotationGenerator\DunglasApiAnnotationGenerator
      namespaces:
        entity: AppBundle\Entity 
      types:
        SocialMediaPosting: ~
        BlogPosting: ~ 
        Article: 
          properties: 
            articleBody: ~
            articleSection: ~
        CreativeWork:
          properties:
            author:
              range: Person
              cardinality: (*..0)
            headline: ~
            isFamilyFriendly: ~
            datePublished: ~
        Thing:
          properties:
            name: ~
        Person:
          properties: {} 
      


  4. We generate classes, we get for free:

    • field documentation is taken from schema.org and translated into PHPDoc
    • Doctrine ORM annotations
    • symfony validation annotations
    • Iri annotations are needed to identify the semantic (semantic) data structure
    • compatible with PSR-2 coding style


    bin/schema generate-types src/ app/config/schema.yml
    


  5. Create a database
    app/console doctrine:database:create
    

  6. And the scheme of our models
    app/console doctrine:schema:create
    

    Generation of models from schemes is supported not only by the Api-platform framework, but also by simple PHP, because you can use schema bundle yourself.
  7. Creating an API
    ApiBundle is directly involved in this. It is enough to indicate only what we want to expose as an API in the form of Symfony services.
    In this guide, these will be the BlogPosting and Person classes
    # app/config/services.yml
    services:
        resource.blog_posting:
            parent:    "api.resource"
            arguments: [ "AppBundle\\Entity\\BlogPosting" ]
            tags:      [ { name: "api.resource" } ]
        resource.person:
            parent:    "api.resource"
            arguments: [ "AppBundle\\Entity\\Person" ]
            tags:      [ { name: "api.resource" } ]
    


    VoilĂ ! Our Api is ready to use.
  8. Check

    app/console server:start
    

    localhost: 8000 / doc
    image

    For convenient work with the API, you can use Postman (its extension for Google Chrome) www.getpostman.com

    Request a list of authors



    Add an author



    Add an article:



    Request a list:



    Check validation. The isFamilyFriendly field must be Boolean.

    {
        "name": "API Platform is great",
        "headline": "You'll love that framework!",
        "articleBody": "The body of my article.",
        "articleSection": "technology",
        "author": "/people/1",
        "isFamilyFriendly": "maybe",
        "datePublished": "2015-05-11"
    }
    

    And we are informed about this in this way:
    {
        "@context": "/contexts/ConstraintViolationList",
        "@type": "ConstraintViolationList",
        "hydra:title": "An error occurred",
        "hydra:description": "isFamilyFriendly: This value should be of type boolean.\n",
        "violations": [
            {
                "propertyPath": "isFamilyFriendly",
                "message": "This value should be of type boolean."
            }
        ]
    }
    

    We fix:
    {
        "name": "API Platform is great",
        "headline": "You'll love that framework!",
        "articleBody": "The body of my article.",
        "articleSection": "technology",
        "author": "/people/1",
        "isFamilyFriendly": true,  
        "datePublished": "2015-05-11"
    }
    

    And the article should be successfully added.



Here is another list of the various features of the main component of the API platform - ApiBundle:


Also popular now: