Build a REST API with Symfony 7

Build a REST API with Symfony 7

In this comprehensive tutorial, we'll delve into building a REST API using Symfony 7, a powerful PHP framework known for its performance and scalability. Symfony 7 empowers developers to construct robust and scalable web applications effortlessly.

Furthermore, we'll leverage the Symfony 7 Local Web Server to run our application efficiently. To facilitate this, ensure you have the Symfony 7 CLI installed. It's recommended to have PHP version 8.1 or above installed for optimal compatibility and performance.

If you're a PHP developer, chances are you're familiar with Symfony, a widely recognized framework in the PHP ecosystem. For those new to Symfony, let's delve into what makes this framework stand out.

Symfony 7 is a highly acclaimed PHP framework renowned for its prowess in simplifying the web development process. Specifically designed to streamline the creation of web applications, Symfony 7 offers a robust backend framework that emphasizes simplicity and efficiency. Moreover, Symfony 7 isn't limited to PHP; it seamlessly integrates with other languages like JavaScript and Node.js, enhancing its versatility and applicability.

API Platform stands out as a comprehensive framework designed for API-first projects, leveraging the robust Symfony components. Let's embark on a journey to create a minimal yet powerful starter project within just 5 minutes!

Setup

  1. Download Symfony Sail: Start by downloading Symfony Sail, which provides a pre-configured development environment tailored for Symfony projects. Extract the downloaded zip into a directory named api-platform-playground.

  2. Create .env.local: Within the api-platform-playground directory, create an empty .env.local file.

  3. Open in Visual Studio Code: Launch Visual Studio Code, type F1, and select Dev Containers: Reopen in Container to open the project within a containerized environment.

Installation

Now that we're set up, let's install the necessary dependencies and configure our project:

# Download and install Symfony skeleton
curl -O https://raw.githubusercontent.com/symfony/skeleton/6.2/composer.json
composer install

# Add required libraries for API Platform documentation navigation
composer require symfony/asset symfony/twig-pack

# Install API Platform package
composer require api-platform/core

Your First Resource

It's time to create our first resource and set up the database:

# Install required packages for resource creation
composer require symfony/validator symfony/orm-pack symfony/maker-bundle

# Set up DATABASE_URL in .env.local
DATABASE_URL="mysql://root:@db:3306/db_name?serverVersion=10.11.2-MariaDB&charset=utf8mb4"

# Generate our first resource named BlogPost
php bin/console make:entity BlogPost --api-resource

During the wizard, ensure to add a required title property with a maximum length of 64 characters. Then, modify App\Entity\BlogPost as follows:

<?php

namespace App\Entity;

use Symfony\Component\Validator\Constraints as Assert;

class BlogPost
{
    #[Assert\NotBlank]
    #[Assert\Length(max: 64)]
    private ?string $title = null;
}

Finally, generate and execute the migration:

php bin/console doctrine:migrations:diff
php bin/console doctrine:migrations:migrate -n

Test Your API

With everything set up, head to http://localhost/api/docs to access the API Platform documentation. Try a simple POST request with a payload like { "title": "My first blog post!" } to test your API.


  • Date: