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
Scheduling tasks & Sending requests
When working on a Laravel API, the task scheduling system plays a significant role in automating background jobs. This can help developers integrate business behavior into their applications more seamlessly.
What is Task Scheduling in Laravel 11?
Task scheduling in Laravel 11 is a feature that allows developers to automate recurring tasks, eliminating the need to manually set up cron jobs. Instead of relying on manual configuration, Laravel 11 provides a more efficient method using console routes, making task scheduling simpler and more streamlined for developers. In the past, developers often used cron jobs manually, but now Laravel offers a much more efficient approach through console routes.
Setting Up Scheduled Tasks
To begin, define any scheduled tasks inside your console routes (routes/console.php
). This file is where you'll handle all the tasks that need to run in the background.
Creating a New Scheduled Task
Here’s an example of scheduling a task that runs every minute:
use Illuminate\Console\Scheduling\Schedule;
$schedule->command('ping')->everyMinute();
You’ll need a command to run this scheduled task, which we’ll create next.
Defining a Custom Command
We’ll create a command for our ping service, which checks multiple services at regular intervals. Run the following Artisan command to create a new command:
php artisan make:command PingCommand
In the PingCommand.php
file, we define the command:
namespace App\Console\Commands;
use Illuminate\Console\Command;
class PingCommand extends Command
{
protected $signature = 'ping';
protected $description = 'Perform a ping check on registered services.';
public function handle(): int
{
$this->info('Starting to ping services...');
// Fetch services and ping them
$services = Service::all();
foreach ($services as $service) {
$this->info('Pinging service: ' . $service->name);
// Your logic for pinging the service
}
return self::SUCCESS;
}
}
With this setup, your command will now ping services every minute.
Managing Large Scale Pings
Handling many pings at once can be overwhelming, especially if you're dealing with thousands or even millions of services.
Here’s how you can efficiently manage these tasks using Laravel’s job system and lazy collections.
Creating a Job to Manage Pings
If you have many pings to handle, it's best to create a job for each ping and dispatch them. Start by creating a job:
php artisan make:job SendPing
Inside the SendPing.php
file, define the job logic:
namespace App\Jobs;
use App\Models\Service;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class SendPing implements ShouldQueue
{
use Dispatchable;
use InteractsWithQueue;
use Queueable;
use SerializesModels;
public function __construct(
public Service $service,
) {}
public function handle(): void
{
// Logic to ping the service
Http::get($this->service->url);
}
}
Now, within your command, dispatch the job for each service:
foreach ($services as $service) {
SendPing::dispatch($service);
}
This approach scales efficiently, even when dealing with a large number of services.
Handling Multiple Instances with Load Balancers
When scaling your Laravel app across multiple instances, such as behind a load balancer, it’s important to ensure that the same command isn't run on multiple instances simultaneously. Laravel provides a way to prevent overlapping of scheduled tasks:
$schedule->command('ping')
->withoutOverlapping()
->onOneServer();
This ensures that only one instance runs the ping command at a time, even if your application is running on multiple servers.
Error Handling and Notifications
It’s always a good idea to handle errors gracefully when working with scheduled tasks and background jobs. Laravel’s try-catch mechanism can help you handle any exceptions that may occur.
try {
Http::get($service->url);
} catch (\Exception $e) {
Log::error('Ping failed for service: ' . $service->name);
}
Additionally, Laravel has built-in support for notifications and error tracking, which can alert you when something goes wrong.