Get Started | Installation | Configuration | Endpoints | Attributes | Commands |
In the Laravel Fast Endpoints (LFE) philosophy, each endpoint is represented as a class. This approach enhances the maintainability and elegance of your API development process. By structuring endpoints as classes, developers can more easily manage and understand the functionality of each endpoint without having to scroll through large controller files to locate and comprehend specific endpoint methods.
An endpoint in LFE is essentially a Laravel Single Action Controller.
This means that the request handling logic is contained within the __invoke
method of the class.
This single-responsibility design pattern makes each endpoint more focused and easier to maintain.
Here’s a simple example of an endpoint class:
<?php
namespace App\Http\Endpoints;
use Baghunts\LaravelFastEndpoints\Endpoint\Endpoint;
use Baghunts\LaravelFastEndpoints\Attributes\{
Get,
Name,
WhereUuid,
}
use Illuminate\Http\Request;
#[WhereUuid('id')]
#[Name('user.find')]
#[Get('/users/{id}')]
class UserShowEndpoint extends Endpoint
{
public function __invoke(Request $request, $id)
{
// Handle the request and return a response
$user = User::findOrFail($id);
return response()->json($user);
}
}
In Laravel Fast Endpoints (LFE), it’s recommended that request and response classes be organized alongside their corresponding endpoint classes. This structure helps maintain a clear and logical organization of your API resources, making it easier for developers to locate and manage related components.
For example, if you have a User and Post resources, the directory structure should look like this:
Http/Endpoints/
User/
Create/
UserCreateRequest.php
UserCreateResponse.php
UserCreateEndpoint.php
Find/
UserFindRequest.php
UserFindResponse.php
UserFindEndpoint.php
Post/
Create/
PostCreateRequest.php
PostCreateResponse.php
PostCreateEndpoint.php
Delete/
PostDeleteRequest.php
PostDeleteResponse.php
PostDeleteEndpoint.php
In this structure:
For LFE to automatically detect and register your endpoint classes,
they must be located in the directory
specified by the dist option in the LFE configuration file config/fast-endpoints.php
.
This directory is where LFE will scan for endpoint classes.
Since the directories may contain other class files,
it is crucial that your endpoint classes extend the Baghunts\LaravelFastEndpoints\Endpoint\Endpoint
abstract class.
This ensures that the endpoint classes have the correct signature required for dynamic route registration.
By extending this abstract class,
the LFE package can accurately detect and register your endpoints based on their configuration and location.
Prev - Configuration | Next - Attributes |