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
Authentication & Authorization
Before we start to allow users to manage their resources and make changes through our API, we need to think about how we can identify our users and restrict them to resources that they own. This is where Authentication and Authorization comes in, allowing us to identify our users and control what they can and cannot do.
In Laravel you have a couple of different options when it comes to Authentication for your API. From Laravel Sanctum, which offers cookie and session based authentication alongside the ability to have API Access Tokens for each user. To Laravel Passport, which is a fully OAuth 2.1 server with all of the bells and whistles. Now this is where the hard part comes in, deciding which package is going to be right for you. You could choose not to use one of these packages, and roll your own, or use an existing 3rd party solution for JWT tokens. However, we will keep it simple here: Sanctum or Passport.
If you know you are going to be building an application that connects to a lot of different systems, in different ways, and you need tight control over this: Passport is 100% what you need. If you are building an API for a couple of integrations, and allowing your users to easily access their data through the API - Sanctum is a lot easier for both you and them to get started.
For this though, we are going to leverage Laravel Sanctum because it has first party support and it pretty much does what JWT would give you but goes a little bit further. That little extra on top of JWT is what actually makes it super useful for your API, until you may need to reach for leveraging OAuth.
With Laravel Sanctum, you can install it into your Laravel API using the following artisan command:
php artisan install:api
This will install Laravel Sanctum, and do a few additional steps to get this set up and ready to use in your Laravel application.
Once you have installed this, we want to make sure that we are protecting all of our API routes using the right middleware.
// routes/api.php
Route::middleware(['auth:sanctum'])->group(static function (): void {
// Your API Routes can now go in here.
});
At this point, our Authentication has been handled. Sanctum defined all of the routes, form requests, controllers - literally everything. Now the code may not be in the way I would approach it, but it is good enough to get you started - focus on the real problems you are trying to solve with your API!
Now, out of the box Laravel comes with an extremely powerful authorization functionality in the form of "Gates". Sanctum will provide you with a way to control abilities, which is basically permissions, for your API tokens. But sometimes you need a little more control than that and need to work with a user basis not tokens.
Our API is likely to have some level of ownership embedded within it, where an authenticated user may own certain resources - or perhaps the user is part of a team or workspace that has the ownership, either way it will be inherited ownership to the user. So we need to look at how to control the ownership, in a way that isn't going to force us to write loads of custom code. Laravel, as usual, has us covered. Laravel has Gates, which is how we can fine-grain control access to certain parts of the application and control if/how we can do certain actions. This is extended to Eloquent Policies, which are classes that control if/how a user can view/modify an eloquent model - which in our API is a resource. Let's create our first policy using artisan:
php artisan make:policy ServicePolicy
Inside here, we can start defining how we want to control the permissions for users. Laravel will hook this up in the background to our corresponding Eloquent Model, we just need to make sure that we fill them in so that we define our access policies for users.
final readonly class ServicePolicy
{
public function view(User $user, Service $service): bool
{
if ( ! $user->hasVerifiedEmail()) {
return false;
}
return $user->id === $service->user_id;
}
}
We can start simple, like the above code example. Can a user view this service. We start by checking if the user has a verified email, if they don't they cannot view this resource. It is important to take a step like email verification to stop anyone from creating a quick account and attempting to access data from your API without first verifying their email at a minimum. Then if they have verified their email address, we can check the ownership of the service against the identity of the user making the request. I find it useful to name these methods in line with the action they are trying to perform such as view
, create
, update
, delete
, archive
, etc etc.
Then within your controller code, you can call these policies using the Gate facade:
Gate::allows('view', $service);
This will return the boolean, allowing you to then handle the success or failure however you see fit. Let's look at a full controller method for this to see it in action.
final class ShowController
{
public function __invoke(Request $request, Service $service): Response
{
if ( ! Gate::allows('view', $service)) {
throw new UnauthorizedAccessException(
message: "You do not have the correct permissions to access this resource.",
status: Response::HTTP_FORBIDDEN,
);
}
return new ModelResponse(
resource: new ServiceResource(
resource: $service,
),
);
}
}
Just like that, we are protecting our resources from being accessed by people we do not explicitly allow access to. We throw an UnauthorizedAccessException
which will bubble up the exception to our Laravel Exception Handler, which is our central place to handle errors and exceptions in our API. This may report it to an error monitoring service, or convert it to a specific API Response. Either way, it isn't logic that needs to live in our controller.