Laravel API Course

1. Intro & Setup

Welcome to the Laravel API course! Get ready to dive into the exciting world of building APIs with Laravel, the powerful and flexible PHP framework.

15:57

2. Cleaning up Laravel

Ready to supercharge your Laravel project for an API-first approach? Let’s dive into optimizing and cleaning it up to make it lightning-fast and ultra-efficient!

16:19

3. Data Modeling

When building an API, mastering the art of structuring your data and defining dynamic relationships between various entities is absolutely essential in Laravel 11.

24:27

4. API Design

When you design an API, it’s absolutely crucial to understand that every API should have a meaningful purpose!

33:09

5. Authentication & Authorization

Say goodbye to the hassle of manually crafting every single detail, and say hello to a streamlined, efficient process that keeps your endpoints vividly documented and ready to go!

14:15

6. Rate Limiting

When it comes to managing API requests, rate limiting is a key player. You don’t want to open the floodgates and let users bombard your API with endless requests.

8:24

7. API Versioning

API versioning might sound like a minor detail at first, but trust me, it's one of those decisions that can make or break the future usability of your API.

22:22

8. Pagination

When you're handling requests that could return extensive lists of resources, it's essential to implement pagination to ensure your responses are manageable and user-friendly.

11:43

9. API Standards

API standards, while useful, aren’t the be-all and end-all. What often takes precedence is discoverability and consistency in your API design.

26:17

10. Handling Errors

Today, we'll explore how to leverage Laravel 11’s error handling system by implementing the API Problem Specification and enhancing it with custom packages to provide consistent, informative error responses.

26:49

11. Write Operations

Let’s explore the steps to manage resources in a Laravel 11 API, focusing on creating, validating, updating, and deleting resources. We’ll use a real-world approach to ensure clarity and usability.

29:30

12. Internationalization

In our previous video, we dove deep into handling write operations—everything from data validation and authorization to deciding between synchronous and asynchronous processing.

11:44

13. Caching Data

In our previous video, we dove deep into handling write operations—everything from data validation and authorization to deciding between synchronous and asynchronous processing.

18:08

14. Basic Security

When it comes to API security, think of it not as a threat but as an opportunity—a chance to build robust, multilayered defenses around your API.

9:53

15. Cache Requests

In Laravel 11, we can take advantage of built-in HTTP features to manage our cache more effectively, streamlining both the response time and the overall user experience.

8:40

16. Scheduling tasks & Sending requests

When working on a Laravel API, the task scheduling system plays a significant role in automating background jobs.

40:33

17. Notifications

Whether you're building a ping service or any application that requires user communication, you’ll likely need to notify users when certain actions occur, such as a service failure.

15:57

18. Monitoring and Logging

When you're developing APIs, it's important to have a clear picture of how your API is performing in real-time.

21:33

19. Testing

When you're developing APIs or any web-based product using Laravel 11, testing plays a crucial role in ensuring that your application behaves as expected.

18:01

20. API Platform

When it comes to building web applications, Laravel has long been a go-to framework for many developers. It's a robust framework, especially for full-stack applications.

17:16

21. Feature Flags

When it comes to building web applications, Laravel has long been a go-to framework for many developers. It's a robust framework, especially for full-stack applications.

11:05

22. Web Sockets

When you're working with APIs, there's a common misconception that WebSockets aren't relevant.

9:58

23. Search API

When you're building APIs, one key feature that often gets overlooked is search. That's what we’ll explore today using TypeSense(https://typesense.org/), a powerful open-source search engine.

15:30

24. Documenting your API

By default, API Platform will give you an OpenAPI specification (currently version 3.1). This standard format allows your API to be easily understood by machines and developers alike.

07:06

Want an easy-to-use API Observability Tool? Try Treblle now

API Platform

About this lesson

When it comes to building web applications, Laravel has long been a go-to framework for many developers. It's a robust framework, especially for full-stack applications.

17:16 October 4, 2024

When it comes to building web applications, Laravel has long been a go-to framework for many developers. It's a robust framework, especially for full-stack applications. But when it comes to building APIs, the Laravel ecosystem hasn't quite hit the same stride as it has for full-stack apps. Now, this is where things get interesting with API Platform—an API-first framework initially created as a Symfony bundle. Over time, API Platform has evolved into its own powerhouse, making API development smooth and intuitive.

In this guide, we’re going to explore how Laravel 11 works with API Platform.

Setting Up Laravel for API Development

To begin, let's set up your Laravel project. If you're familiar with Laravel, you'll know it’s pretty flexible, but if you're new, don't worry—this guide will walk you through the essentials.

Install Laravel: You can create a new Laravel project using Composer. If you're not familiar, here’s how you can get started:

composer create-project --prefer-dist laravel/laravel api-example

Install API Platform for Laravel: API Platform is the tool we're focusing on. However, it's still under heavy development. You can install the required dependencies using Composer:

composer require api-platform/laravel

Note: Keep in mind that API Platform for Laravel isn’t a fully stable release yet, but it's worth exploring. Once ready, it will integrate seamlessly into your projects. Stay updated by checking out the API Platform documentation.

Defining API Resources in Laravel

Laravel's models make it super easy to start working with your database. Let’s walk through adding an API resource to your Laravel application:

Create a Model: For example, let’s create a Car model to demonstrate how this works with Laravel’s Eloquent ORM.

php artisan make:model Car -m

Add Model Attributes: Define your model with attributes like make, model, and year in the migration file:

$table->string('make');
$table->string('model');
$table->unsignedInteger('year');

Run the migration:

php artisan migrate

API Resource: Now, to expose this Car model as an API resource, Laravel's integration with API Platform makes it a breeze. Add an ApiResource attribute to the model like this:

use ApiPlatform\Core\Annotation\ApiResource;

#[ApiResource]
class Car extends Model
{
    // Model code
}

For more advanced resource configurations, refer to the Laravel API Resources documentation.

Pagination and Filtering

Pagination is important for managing large sets of data. With Laravel and API Platform, this is automatic. For example, if you want to fetch a list of Cars, the pagination kicks in out of the box.

Paginated API Response: Once you’ve seeded some data, querying the API automatically paginates results:

php artisan db:seed

Now, if you hit the /api/cars endpoint, you’ll get a paginated response.

Filtering Resources: API Platform allows you to add filters based on resource fields. For example, if you want to filter cars by year, API Platform offers the ApiFilter attribute. Here's how you could filter cars by year:

#[ApiResource]
#[QueryParameter(key: 'year', filter: EqualsFilter::class)]
class Car extends Model
{
    // Model code
}

You can learn more about filters and their capabilities in the API Platform Filter Documentation.

Testing and Working with Data

One of the great things about Laravel is the ease of testing APIs. You don’t need any complex setup. Let’s quickly cover how you can test your API and work with seeded data.

Seeding Data: Seed your database with test data for cars and users:

php artisan migrate:fresh --seed

This command will refresh the database and populate it with sample data for easy testing.

Testing Endpoints: You can use Laravel’s built-in testing capabilities or external tools like Aspen to test your API.

Here’s how you would test the /api/cars endpoint:

curl -X GET http://localhost:8000/api/cars

If everything’s set up correctly, you should see a paginated list of cars, with all the data nicely structured.

Leveraging API Platform Features

API Platform brings a lot to the table, and while it's still in early stages with Laravel, it's definitely worth understanding what’s possible.

With API Platform, you get out-of-the-box support for:

  • Serialization: Automate transforming your model data to JSON or XML formats.
  • Content Negotiation: Allows clients to request responses in their preferred format (e.g., JSON, XML).
  • Pagination, Filtering, and Sorting: Simplifies data handling for large datasets.

All these features help streamline API development, allowing you to focus more on building logic rather than worrying about implementation details.

Check out the API Platform Features Documentation for a deeper dive.

Conclusion

While Laravel has always been known for web application development, with the integration of API Platform, it’s clear that Laravel is stepping up its API game. You can expect to build powerful, flexible APIs with minimal effort, leveraging the strengths of both frameworks.

As a developer, I find this combination to be incredibly exciting. With Laravel 11, you get to enjoy a top-notch developer experience while pushing the boundaries of what's possible in API development.

For more on API Platform’s integration with Laravel, keep an eye on the API Platform repository for updates. And if you want to dive deeper into the world of APIs, I highly recommend exploring the Treblle API Insights for monitoring and enhancing your API projects.