REST API: platform API

July 3, 2019

Lang: cs en de es

How to run a web application as a REST API? This is where the API Platform library comes in handy. Here I will describe how to make a REST APi using Symfony and the API Platform library.

The REST API has been popular for a while now. Some places have already seen the wave of this technology, some don't even know what it is yet. It's nothing more than using the HTTP protocol as the basis for defining a standard. More about REST on Wikipedia.

When there is a standard, libraries are created that allow the technology to be used efficiently. Efficiently means that:

  • the design of the technology/library will be better because more people have been involved
  • I don't have to re-write what's already done
  • someone maintains the library
  • more people have seen the code
This allows the programmer to focus on learning the technology, the library, and implementing the technology in his application.

One of the REST API implementations for PHP is API Platform, which is primarily developed for the Symphony framework.

Rather than going into a lengthy description of what and how, I'll show a few examples and details of the usage are in the video at the end of this article. For more detailed information I recommend wikipedia, google and documentation.

Requirements

To get started you will need:

  • composer
  • php7
  • symphones, doctrine
  • database - e.g. mysql

Use


How to get started with REST and using the Platform API? Simply here is a small demo.

Setting up a project:

$ composer create-project symfony/skeleton bookshop-api
$ cd bookshop-api

$ composer req api
Set up the database connection:
$ export DATABASE_URL=mysql://db_user:db_password@127.0.0.1:3306/db_name
Create a database and a database user with rights:
MariaDB [(none)]> create database db_name;
MariaDB [(none)]> create user db_user@localhost identified by 'db_password';
GRANT ALL PRIVILEGES ON db_name. * TO 'db_user'@'localhost';


MariaDB [(none)]> flush privileges;
$ bin/console doctrine:schema:create
Start the application:
$ php -S *1:8000 -t public
The Symfony application is then available at:
http://192.165.85.12:8000
The API is available at:
http://192.165.85.12:8000/api
Create according to the two already defined entity files:
bin/console doctrine:schema:update --force
We will add annotations to the PHP code for the entity:
use ApiPlatform\Core\Annotation\ApiResource;
/**
 * ...
 *
 * @ApiResource
 */
 
And now we can test the API via POST and GET HTTP requests on the URL with the visual API presentation. api-platform-rest.png
{
  "isbn": "9781782164104",
  "title": "Persistence in PHP with the Doctrine ORM",
  "description": "This book is designed for PHP developers ... better understanding of Persistence and ORM,
  "author": "Kévin Dunglas",
  "publicationDate": "2013-12-01"
}
{
    "book": "/books/1",
    "rating": 5,
    "body": "Interesting book!",
    "author": "Kévin",
    "publicationDate": "September 21, 2016"
}
api-platform-entita.png

Video

Live-stream with a practical deployment demonstration:

Resources

resource and documentation api platforms

Articles on a similar topic

Go programming language
Analysis of assignment and pricing of software project development
Python program to control Docker using the API
How to use MailCatcher to test emails
Python OpenAI API
Creating a WebSocket web application and setting up a proxy
Project management: agile software development
How to run old PHP applications
What a good programmer should know
Rust programming language
NodeJS: development, server configuration
Nette security bug CVE-2020-15227
Custom web and mail hosting with ISP Config software
Programming in SQL: PostgreSQL, MySQL/MariaDB
HTTPS: secure web
NoSQL database Mongo DB
Connecting to Microsoft SQL Server from Linux
What is the job description of a programmer
Python application localization
Which mail and web hosting to choose
Digispark - Program Atmel ATtiny microcontroller with Arduino IDE
Development for ARM processors with Arduino IDE
How to program the ESP8266 WiFi processor
Open smartphone with Linux - Openmoko Neo FreeRunner

Newsletter

If you are interested in receiving occasional news by email.
You can register by filling in your email news subscription.


+