Laravel FastAPI is a PHP attribute-based routing package designed to streamline API development. It enables developers to define routes, methods, and middlewares directly within their controllers using simple PHP attributes. By reducing the complexity of traditional route definition, Laravel FastAPI improves code organization and enhances performance with route caching. Perfect for developers looking to build clean, efficient APIs in Laravel.
mkd/laravel-fast-api is a Laravel package for laravel fastapi is a php attribute-based routing package designed to streamline api development. it enables developers to define routes, methods, and middlewares directly within their controllers using simple php attributes. by reducing the complexity of traditional route definition, laravel fastapi improves code organization and enhances performance with route caching. perfect for developers looking to build clean, efficient apis in laravel..
It currently has 12 GitHub stars and 8 downloads on Packagist (latest version v0.0.1).
Install it with composer require mkd/laravel-fast-api.
Discover more Laravel packages by mkd
or browse all Laravel packages to compare alternatives.
Last updated
Laravel FastAPI is a PHP attribute-based routing solution for building APIs quickly and efficiently. With FastAPI attributes, you can define routes, methods, and middlewares directly in your controller classes, reducing the need for complex route files and enabling better organization and clarity.
This package also integrates seamlessly with Laravel's route:cache for enhanced performance, ensuring your APIs are as fast as possible.
FastApiMethod for your HTTP methods.route:cache for optimal performance.You can install the package via Composer:
composer require mkd/laravel-fast-api
Use the #[FastAPIGroup] and #[FastAPI] attributes to define routes inside your controller classes.
use MKD\FastAPI\Attributes\FastAPI;
use MKD\FastAPI\Attributes\FastAPIGroup;
#[FastAPIGroup(prefix: '/items', options: ['name' => 'items'], middlewares: ['auth'])]
class ItemsController extends Controller
{
#[FastAPI(method: FastApiMethod::GET, path: '/data/{id}', options: ['functions' => [
'whereIn' => ['id', ['2']],
], 'name' => 'item_id'])]
public function getItem($id)
{
return response()->json(['item' => $id]);
}
}
This simple attribute-based approach automatically handles routing logic, allowing you to focus on building your API logic.
You can define routes with the following HTTP methods:
enum FastApiMethod
{
case GET;
case POST;
case PUT;
case PATCH;
case OPTION;
case DELETE;
case ANY;
case REDIRECT;
case MATCH;
}
In addition, you can leverage these functions to customize your routes:
private array $supportedFunctions = [
'middleware',
'where',
'whereNumber',
'whereAlpha',
'whereAlphaNumeric',
'whereUuid',
'whereUlid',
'whereIn',
'name',
'withTrashed',
'scopeBindings',
'withoutScopedBindings',
];
In your configuration file, you can specify paths and controllers to be scanned for FastAPI attributes.
return [
//Paths to check for controllers that use fast-api
'paths' => [
app_path('Http/Controllers'),
],
//Specify controllers that are not included in the paths
'controllers' => [
\App\Http\Controllers\CustomController::class
],
];
FastAPI provides useful commands for caching and clearing controllers:
Cache the controllers for better scanning performance:
php artisan fast-api:cache
Clear the cached controllers:
php artisan fast-api:clear-cache
By using Laravel's route:cache, the FastAPI routes are cached to ensure high performance. It is recommended to always cache your routes in production environments for faster API responses.
php artisan route:cache
Feel free to submit issues and pull requests. Contributions are welcome!
The MIT License (MIT). Please see License File for more information.