📜 ⬆️ ⬇️

Api-platform

image
With the approach of Web 3.0, the first and Linked Data (or something like Sematic Web) API approaches are increasingly being used. In this regard, readers are invited to get acquainted with the framework for easy creation of an API based on the schemes collected on schema.org with the generation of responses in the form of JSON-LD ,

This is a short version of the Api-platform tutorial.


  1. Install the project with Composer
    composer create-project api-platform/api-platform blog-api 

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

  3. Creating a data model
    1. Api-platform contains a model generator from the schema.org dictionary
    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, which types to create and which fields we need (because Schema.org (http://schema.org/) presents the most complete entity schemas and usually we need only a certain subset of the described fields) . In addition, there is the possibility of describing 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 

    Generating models from schemas is supported not only by the Api-platform framework, but also by simple PHP, because you can use the schema bundle yourself.
  7. We create API
    This is done directly by ApiBundle. Just specify that we want to set up as an API as a symfony service.
    In this tutorial, these will be 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

    We request the list of authors



    We add the author



    Adding an article:



    We request the list:



    We check validation. The field isFamilyFriendly 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 it 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 added successfully.



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

Source: https://habr.com/ru/post/269377/


All Articles