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

Notifications

About this lesson

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 October 4, 2024

In this section, we will see how Laravel 11 handles notifications and emails within an API context. 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. Let's walk through the process of generating a notification, sending it via email, and integrating different communication channels like Slack or SMS.

Setting Up Notifications

We all use emails, but sometimes we need more dynamic notifications. In our example, when a check fails in a ping service, we want to notify the user immediately. Laravel makes it simple to send notifications through various channels.

php artisan make:notification CheckFailed

This command generates a notification class called CheckFailed. Now, we’ll decide how to send this notification—via email, Slack, or SMS. For now, let's focus on email.

Find out more about notifications in Laravel Notifications Documentation

Sending Emails When a Check Fails

In our example, whenever a check fails, we’ll trigger an email alert. Here’s how we can configure the email notification:

public function toMail($notifiable)
{
    return (new MailMessage)
        ->subject('Service Check Failed')
        ->line('We just checked your service, and it seems to be down.')
        ->action('Check Service Status', url('/'))
        ->line('Thank you for using our application!');
}

This email will be sent whenever a check fails, alerting the user about the service downtime.

Tip: Consider using queues to handle email notifications asynchronously to prevent performance issues. Learn more about Laravel Queues.

Handling Status Codes

We want to ensure that our notification only triggers when a specific HTTP status code is returned, such as a 200 OK. If the service returns anything else (e.g., 404 Not Found), the notification will be triggered.

if ($response->status() !== 200) {
    $user->notify(new CheckFailed($check));
}

By checking the status code, we ensure that we only send notifications when the service fails to respond as expected.

Optimizing Queue Handling

One of the best practices in Laravel is to use queues for tasks like sending emails. It prevents the application from slowing down when multiple notifications are being processed.

class CheckFailed extends Notification implements ShouldQueue
{
    // Notification logic here
}

By implementing the ShouldQueue interface, you’re telling Laravel to handle this notification in the background, improving overall performance.

Learn more about Laravel Queues.

Using Observers to Trigger Notifications

Laravel Observers are another powerful tool to automate tasks. In our example, we can create an observer that triggers when a new report is created.

php artisan make:observer ReportObserver

In this observer, we'll notify the user when a report is created and associated with a failed check:

public function created(Report $report): void
{
    $user = $report->check->service->user;
    $user->notify(new CheckFailed($report->check));
}

This ensures that users are notified immediately whenever a new report indicates a failure in the system.

Want to learn more? Check out the official Laravel Observers Documentation.

Multiple Notification Channels

Laravel supports sending notifications across multiple channels, not just email. If you need to notify users via Slack, SMS, or WebSockets, Laravel has built-in support for these methods. You can even combine channels to ensure users get notified no matter what.

public function via($notifiable): array
{
    return ['mail', 'slack'];
}

This allows you to send the same notification via email and Slack, ensuring that users are always in the loop.

Learn more: Laravel Notification Channels

Summary

In this segment, we’ve explored how to send notifications and emails using Laravel 11. From triggering notifications when a service check fails to optimizing performance with queues, Laravel provides a flexible system to manage user alerts. Don't forget to check out the official Laravel SDK and explore further channels like WebSockets for real-time notifications.