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

Feature Flags

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.

11:05 October 4, 2024

When you're building APIs that need to scale or roll out new features to a limited set of users, feature flags come in handy. In this section, we will see how to implement feature flags within a Laravel 11 API, leveraging a first-party package called Laravel Pennant.

If you've worked with JavaScript before, you’re probably familiar with feature flag services like LaunchDarkly or Split. Laravel, though, has its own clean, efficient way to handle feature flags natively, and we’re going to explore that.

Getting Started with Laravel Pennant

First things first, we need to install the Laravel Pennant package, which is Laravel’s lightweight and highly effective solution for feature flags.

composer require laravel/pennant

Once that's done, you'll want to publish the configuration files and the database migrations that come with the package.

php artisan vendor:publish --tag=laravel-pennant-config
php artisan migrate

This step will set up everything you need in the background, including a database table to store your feature flags.

Pro Tip: If you want to explore other feature flagging options for your Laravel application, check out API Insights, which offers great tips on enhancing API functionality.

Defining Your Feature Flags

Now that we’ve got the package installed, let’s define a feature flag. We'll start by creating a new service provider, so we don't clutter up your AppServiceProvider.

php artisan make:provider FeatureServiceProvider

In the FeatureServiceProvider, we’ll define our features inside the boot() method. Here’s an example that flags a group of beta testers:

public function boot()
{
    Feature::define('beta-tester', function (User $user): bool {
        return $user->hasActivePlan();
    });
}

Here, we’re checking if a user has an active plan (using something like Laravel Cashier for subscription management), and allowing them to access beta features based on that condition. It’s easy to customize this logic further depending on your use case.

For more about managing subscriptions, check out the Laravel Cashier’s documentation.

Using Feature Flags in Controllers

Now, how do we use these flags within our API? It’s pretty straightforward. In your controllers, you can check whether a feature is active or not and adjust the API response accordingly.

public function show(User $user)
{
    if (Feature::active('beta-tester', $user)) {
        return new ApiBetaResponse();
    }

    return new ApiResponse();
}

This is where feature flags become truly powerful — you can gate certain parts of your API or change functionality based on user roles, plans, or other conditions.

Middleware for Feature Flags

Laravel Pennant also allows you to use middleware to control access based on feature flags. For example, you might want to restrict access to a specific route for users with the 'beta-tester' flag:

Route::middleware('feature:beta-tester')->group(function () {
    Route::get('/test', function () {
        return view('test');
    });
});

This way, only users with the appropriate flag can access the route.

Want to dig deeper? Check out the Laravel Pennant documentation.

Error Handling and Custom Responses

Handling error states when a feature flag is inactive is crucial for a smooth user experience. Laravel Pendant allows you to define custom error responses, so when users don’t meet the criteria for accessing a feature, they get appropriate feedback.

In the service provider, you can define a custom response like this:

Feature::define('beta-tester', function (User $user): bool {
    return $user->hasActivePlan();
})->onInactive(function ($request, $response) {
    return response()->json(['message' => 'This feature is not available to you.'], 403);
});

This approach ensures that users without the required feature flag get an informative response, rather than a generic error.

Why Version Control for Feature Flags?

One of the biggest benefits of using Laravel Pendant is version control. If a feature flag breaks something in production, you can easily roll back to a previous state. This is incredibly useful when working in environments with high user interaction and frequent updates.

Want to explore more best practices? Take a look at the Laravel SDK for other powerful tools you can integrate with your API.