Magic of API Platform Comes to Laravel: Build an API in Just 5 Minutes Instead of Days

Magic of API Platform Comes to Laravel

20 November

As developers, we are constantly searching for tools and platforms that simplify complex processes and enable us to build better applications faster. One of the most exciting new additions to the Laravel ecosystem is the API Platform, a powerful toolkit previously well-known in the Symfony world, now available for Laravel. This means Laravel developers can now enjoy seamless API development with a full set of features that make building modern, scalable APIs simpler than ever.

In this blog, we’ll explore what the API Platform is, why it’s a great addition to the Laravel ecosystem, and how you can start using it in your Laravel projects.

What Is API Platform?

API Platform is an open-source framework designed to help developers easily build REST and GraphQL APIs. Initially popular in the Symfony community, API Platform automates much of the boilerplate code and complexities involved in creating secure, feature-rich APIs. It offers out-of-the-box features like automatic API documentation, serialization, data validation, and pagination.

With the new integration, Laravel developers can now leverage API Platform’s powerful tools, enabling them to build scalable and standardized APIs more efficiently, all while sticking to Laravel’s beautiful syntax and ecosystem.

Why Use API Platform in Laravel?

  1. Automatic API Generation: One of the most magical features of the API Platform is its ability to automatically generate REST and GraphQL endpoints from your models and database schemas. With just a few configurations, you can turn your Laravel entities into fully functional APIs.
  2. Built-In Swagger Documentation: API Platform comes with Swagger and OpenAPI documentation generation out of the box. This means that as soon as you create your API, the platform automatically generates an interactive documentation interface. No need to manually write API docs—API Platform handles it for you!
  3. GraphQL Support: If you’re working with GraphQL, API Platform supports that too! It allows you to expose your Laravel data through a GraphQL API with almost no extra configuration.
  4. Pagination, Filtering, and Sorting: API Platform includes robust pagination, filtering, and sorting features, letting you add advanced querying capabilities to your API with minimal effort.
  5. Security and Validation: Security and data validation are crucial for any API, and API Platform has this covered. It integrates seamlessly with Laravel’s validation rules and middleware, allowing you to secure your endpoints and validate incoming requests without reinventing the wheel.
  6. Event-Driven Architecture: API Platform allows you to create custom event listeners and handlers in your API process, making it easier to handle side effects such as logging, notifications, or database updates whenever certain events are triggered within your API calls.
  7. Open Standards: One of the best parts about using API Platform is its commitment to open standards. It supports HAL, JSON-LD, and GraphQL natively, providing flexibility in how your data is structured and consumed by clients.

Peanut Square LLP: Experts in API Platform Development

At Peanut Square LLP, we have been using API Platform for the last eight years, helping hundreds of customers speed up API development with 90% less development time and reducing the time to create new APIs by 50%. Our team specializes in building scalable, feature-rich APIs for businesses of all sizes, from startups to large enterprises. Whether you’re working on a new project or need to streamline an existing one, we can help you harness the power of API Platform to build faster and more efficient APIs.

Getting Started with API Platform in Laravel

Installing Laravel

API Platform can be installed easily on new and existing Laravel projects. If you already have an existing project, skip directly to the next section. If you don’t have an existing Laravel project, create one.

All Laravel installation methods are supported.

For instance, you can use Composer:

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

Installing API Platform

In your Laravel project, install the API Platform integration for Laravel:

composer require api-platform/laravel

After installing API Platform, publish its assets and config:

php artisan api-platform:install

If it’s not already done, start the built-in web server:

php artisan serve

Open http://127.0.0.1:8000/api/

your API is already active and documented… but empty!

Publishing the Config File and Assets

After installing API Platform, you can publish its assets and config using the api-platform:install Artisan command.

php artisan api-platform:install

Creating an Eloquent Model

To discover how API Platform framework works, we will create an API to manage a Blog Post.

Let’s start by creating a Post model:

php artisan make: model Post

Creating a Migration

First, create a migration class for the posts table:

php artisan make:migration create_posts_table

Open the generated migration class (database/migrations/<timestamp>_create_posts_table.php) and add some columns:

    public function up(): void
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->id();

+            $table->string('title');
+            $table->text('description');
+            $table->string('author');
+            $table->date('publication_date')->nullable();

            $table->timestamps();
        });
    }

Finally, execute the migration:

php artisan migrate

The table and columns have been created for you!

Exposing A Model

Open app/Models/Post.php that we generated in the previous step and mark the class it contains with the

#[ApiResource] attribute:

namespace App\Models;
use ApiPlatform\Metadata\ApiResource;
use Illuminate\Database\Eloquent\Model;

#[ApiResource]
 class Post extends Model
 {
 }

Open http://127.0.0.1:8000/api/, your API is ready and completely functional!

You can play with your API with the SwaggerUI.

For More information Read Here

Here is the Github repo for this Demo

https://github.com/PeanutSquareLLP/laravel-api-platform-demo

Customize Your API

API Platform is highly customizable. You can add custom routes, define serialization groups, and even create custom data providers or processors for more complex use cases. The platform’s flexibility means you can tailor your API exactly to your project’s needs without sacrificing performance or maintainability.

Conclusion

API Platform’s availability in Laravel is a game-changer. It streamlines API development, offering features like automatic documentation, GraphQL support, and security out of the box. If you’re looking to simplify the process of building APIs while maintaining the power and flexibility of Laravel, API Platform is definitely worth checking out.

At Peanut Square LLP, we have mastered the art of API development using API Platform, helping our clients accelerate their projects with maximum efficiency. Contact Peanut Square LLP for your next project and experience the magic of faster API development!

Ready to give it a try? Head over to the API Platform documentation and start integrating it into your Laravel applications today.

php