https://github.com/mustafakhaleddev/laravel-fast-api
Laravel 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
https://github.com/mustafakhaleddev/laravel-fast-api
api fastapi laravel laravel-framework laravel-package
Last synced: 5 months ago
JSON representation
Laravel 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
- Host: GitHub
- URL: https://github.com/mustafakhaleddev/laravel-fast-api
- Owner: mustafakhaleddev
- License: mit
- Created: 2024-10-16T19:43:19.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-10-16T19:50:32.000Z (over 1 year ago)
- Last Synced: 2025-07-21T18:15:15.576Z (11 months ago)
- Topics: api, fastapi, laravel, laravel-framework, laravel-package
- Language: PHP
- Homepage:
- Size: 8.79 KB
- Stars: 11
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Laravel Fast-API
[](https://github.com/mustafakhaleddev/laravel-fast-api/releases)
[](https://github.com/mustafakhaleddev/laravel-fast-api/issues)
[](https://github.com/mustafakhaleddev/laravel-fast-api/blob/main/LICENSE)
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.
## Features
- **Attribute-Based Routing**: Define your API routes using PHP attributes.
- **Support for Advanced Routing Options**: Middleware, where clauses, route options, and more!
- **Enum-Based HTTP Methods**: Use the predefined `FastApiMethod` for your HTTP methods.
- **API Caching**: Leverage Laravel's `route:cache` for optimal performance.
- **Clear API Cache**: Quickly clear cached API routes with simple Artisan commands.
## Installation
You can install the package via Composer:
```bash
composer require mkd/laravel-fast-api
```
## Usage
### Define FastAPI Routes
Use the `#[FastAPIGroup]` and `#[FastAPI]` attributes to define routes inside your controller classes.
```php
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.
### Supported Methods and Functions
You can define routes with the following HTTP methods:
```php
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:
```php
private array $supportedFunctions = [
'middleware',
'where',
'whereNumber',
'whereAlpha',
'whereAlphaNumeric',
'whereUuid',
'whereUlid',
'whereIn',
'name',
'withTrashed',
'scopeBindings',
'withoutScopedBindings',
];
```
### Configurations
In your configuration file, you can specify paths and controllers to be scanned for FastAPI attributes.
```php
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
],
];
```
### Artisan Commands
FastAPI provides useful commands for caching and clearing controllers:
- Cache the controllers for better scanning performance:
```bash
php artisan fast-api:cache
```
- Clear the cached controllers:
```bash
php artisan fast-api:clear-cache
```
### Performance
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.
```bash
php artisan route:cache
```
## Contributing
Feel free to submit issues and pull requests. Contributions are welcome!
## License
The MIT License (MIT). Please see [License File](LICENSE) for more information.
---