API Platform Now Available for Laravel: A Game-Changer for API Development

API Platform Now Available for Laravel: A Game-Changer for API Development

Posted By

kamlesh paul

on

Dec 14, 2024

Table of contents

Introduction

At API Platform Con 2024, Kévin Dunglas, the creator of API Platform, announced that API Platform is now officially available for Laravel. This exciting development brings the powerful API-building capabilities of API Platform to one of the most popular web frameworks—Laravel. This integration will streamline the API development process, enabling developers to create advanced APIs with minimal effort.

Why API Platform for Laravel is a Big Deal:

Laravel has long been known for its simplicity and power, but adding API Platform into the mix raises its API development capabilities to a new level. API Platform, with its out-of-the-box features like automatic documentation, OpenAPI support, GraphQL, and strict adherence to modern API standards, now seamlessly integrates with Laravel.

Key Features of API Platform in Laravel:

  • Composer Installation: Getting started is simple. Install API Platform using Composer (composer require api-platform/laravel) and start building your API in any existing or new Laravel project.
  • Seamless Laravel Integration: The Laravel package registers API Platform services within Laravel’s Dependency Injection Container. It also integrates smoothly with Laravel’s ecosystem, including Eloquent, Validation, Policies, Gates, and more.
  • Routing and Documentation: API routes are automatically generated, and the OpenAPI documentation is provided out of the box.
  • GraphQL Support: State-of-the-art GraphQL support, implementing the Relay specification, is available for developers who prefer building GraphQL APIs.

Step-by-Step Guide to Try API Platform in Laravel

Follow these steps to create your first API using API Platform in a Laravel project:

  1. Set Up a Laravel Project:

If you don’t have a Laravel project yet, create a new one:

composer create-project laravel/laravel my-api
cd my-api

Or, if you already have an existing Laravel project, navigate to its directory.

  1. Install API Platform:

Install the API Platform package for Laravel using Composer:

composer require api-platform/laravel
  1. Start Laravel’s Development Server:

To ensure your Laravel app is working, you can start the built-in development server:

php artisan serve
  1. Create an Eloquent Model for Your API:

Create a model that represents the data you want to expose through your API. For this example, we will create a Book model:

php artisan make:model Book -m

This command will generate a model and migration.

  1. Edit Migration:

Edit the generated migration file (in the database/migrations folder) to define the columns for your books table:

Schema::create('books', function (Blueprint $table) {
    $table->id();
    $table->string('isbn')->nullable();
    $table->string('title');
    $table->text('description');
    $table->string('author');
    $table->date('publication_date')->nullable();
    $table->timestamps();
});

Run the migration to create the table:

php artisan migrate
  1. Annotate the Model for API Platform:

Now, let’s tell API Platform that we want the Book model to be an API resource. Open the Book model (app/Models/Book.php) and add the #[ApiResource] attribute:

Book.php
<?php
 
namespace App\Models;
 
use ApiPlatform\Metadata\ApiResource;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
 
#[ApiResource]
class Book extends Model
{
    use HasFactory;
 
    // You can also hide or expose specific fields if needed
    protected $hidden = ['isbn'];  // Optional
}
  1. Access Your API:

API Platform will automatically expose CRUD endpoints for the Book model, including GET, POST, PUT, DELETE, etc. You can now visit the following URL to view the generated API for your model:

http://127.0.0.1:8000/api/books

This will return a list of all Book resources in JSON-LD format, as API Platform follows modern API standards like JSON-LD and OpenAPI.

API-Platform-api-books.webp

  1. View the OpenAPI Documentation

API Platform automatically generates OpenAPI documentation for your API. You can view it by visiting:

http://127.0.0.1:8000/api/docs

This will display an interactive Swagger UI where you can test your API endpoints.

Why Laravel Developers Should Care:

For Laravel developers, this integration offers a powerful way to create APIs that adhere to best practices, such as OWASP security standards, JSON:API support, and even HATEOAS/hypermedia. It also brings native support for authentication with Laravel Sanctum, Passport, and Socialite, allowing for flexible user authentication and authorization setups.

API Platform’s arrival in Laravel makes it a powerful tool for API-first applications. With the support of modern API standards, rich documentation, and real-time capabilities, Laravel developers now have everything they need to create scalable, efficient APIs without compromising on best practices.


FAQ

Q1: Can I use API Platform with an existing Laravel project?

Yes, API Platform can be installed in any existing Laravel project using Composer. Simply run composer require api-platform/laravel and you’ll be able to start building APIs right away.

Q2: Do I need to use the entire API Platform, or can I just use specific features?

You can use as much or as little of API Platform as you need. While API Platform provides a complete solution for API development (including REST, GraphQL, documentation, and more), you can choose to only implement specific features based on your project requirements.

Q3: Does API Platform support GraphQL in Laravel?

Yes, API Platform offers built-in GraphQL support, implementing the Relay specification. You can enable GraphQL in the API Platform configuration and start using it alongside your REST endpoints.

Q4: How does API Platform integrate with Laravel’s authentication and authorization?

API Platform natively integrates with Laravel’s authentication mechanisms, including Laravel Sanctum, Passport, and Socialite. You can manage access control using Laravel’s policies and gates, just as you would in a standard Laravel application.

Q5: What URLs does API Platform generate?

For every model annotated with #[ApiResource], API Platform automatically generates a set of CRUD (Create, Read, Update, Delete) endpoints. For example, if you create a Book model, the following URLs will be available:

  • GET /api/books retrieves a list of all books.
  • POST /api/books creates a new book.
  • GET /api/books/{id} retrieves a specific book by ID.
  • PUT /api/books/{id} updates a specific book by ID.
  • DELETE /api/books/{id} deletes a specific book by ID.

You can access these URLs at http://127.0.0.1:8000/api/books or another base URL depending on your setup.

Q6: What types of API documentation are generated?

API Platform automatically generates OpenAPI (formerly known as Swagger) documentation, which you can access via /api/docs. This provides an interactive API documentation interface, where developers can explore and test API endpoints.

Share this article

36 views