|
| 1 | +# Laravel Fast-API |
| 2 | + |
| 3 | +[](https://github.com/mustafakhaleddev/laravel-fast-api/releases) |
| 4 | +[](https://github.com/mustafakhaleddev/laravel-fast-api/issues) |
| 5 | +[](https://github.com/mustafakhaleddev/laravel-fast-api/blob/main/LICENSE) |
| 6 | + |
| 7 | +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. |
| 8 | + |
| 9 | +This package also integrates seamlessly with Laravel's `route:cache` for enhanced performance, ensuring your APIs are as fast as possible. |
| 10 | + |
| 11 | +## Features |
| 12 | + |
| 13 | +- **Attribute-Based Routing**: Define your API routes using PHP attributes. |
| 14 | +- **Support for Advanced Routing Options**: Middleware, where clauses, route options, and more! |
| 15 | +- **Enum-Based HTTP Methods**: Use the predefined `FastApiMethod` for your HTTP methods. |
| 16 | +- **API Caching**: Leverage Laravel's `route:cache` for optimal performance. |
| 17 | +- **Clear API Cache**: Quickly clear cached API routes with simple Artisan commands. |
| 18 | + |
| 19 | +## Installation |
| 20 | + |
| 21 | +You can install the package via Composer: |
| 22 | + |
| 23 | +```bash |
| 24 | +composer require mkd/laravel-fast-api |
| 25 | +``` |
| 26 | + |
| 27 | +## Usage |
| 28 | + |
| 29 | +### Define FastAPI Routes |
| 30 | + |
| 31 | +Use the `#[FastAPIGroup]` and `#[FastAPI]` attributes to define routes inside your controller classes. |
| 32 | + |
| 33 | +```php |
| 34 | +use MKD\FastAPI\Attributes\FastAPI; |
| 35 | +use MKD\FastAPI\Attributes\FastAPIGroup; |
| 36 | + |
| 37 | +#[FastAPIGroup(prefix: '/items', options: ['name' => 'items'], middlewares: ['auth'])] |
| 38 | +class ItemsController extends Controller |
| 39 | +{ |
| 40 | + #[FastAPI(method: FastApiMethod::GET, path: '/data/{id}', options: ['functions' => [ |
| 41 | + 'whereIn' => ['id', ['2']], |
| 42 | + ], 'name' => 'item_id'])] |
| 43 | + public function getItem($id) |
| 44 | + { |
| 45 | + return response()->json(['item' => $id]); |
| 46 | + } |
| 47 | +} |
| 48 | +``` |
| 49 | + |
| 50 | +This simple attribute-based approach automatically handles routing logic, allowing you to focus on building your API logic. |
| 51 | + |
| 52 | +### Supported Methods and Functions |
| 53 | + |
| 54 | +You can define routes with the following HTTP methods: |
| 55 | + |
| 56 | +```php |
| 57 | +enum FastApiMethod |
| 58 | +{ |
| 59 | + case GET; |
| 60 | + case POST; |
| 61 | + case PUT; |
| 62 | + case PATCH; |
| 63 | + case OPTION; |
| 64 | + case DELETE; |
| 65 | + case ANY; |
| 66 | + case REDIRECT; |
| 67 | + case MATCH; |
| 68 | +} |
| 69 | +``` |
| 70 | + |
| 71 | +In addition, you can leverage these functions to customize your routes: |
| 72 | + |
| 73 | +```php |
| 74 | +private array $supportedFunctions = [ |
| 75 | + 'middleware', |
| 76 | + 'where', |
| 77 | + 'whereNumber', |
| 78 | + 'whereAlpha', |
| 79 | + 'whereAlphaNumeric', |
| 80 | + 'whereUuid', |
| 81 | + 'whereUlid', |
| 82 | + 'whereIn', |
| 83 | + 'name', |
| 84 | + 'withTrashed', |
| 85 | + 'scopeBindings', |
| 86 | + 'withoutScopedBindings', |
| 87 | +]; |
| 88 | +``` |
| 89 | + |
| 90 | +### Configurations |
| 91 | + |
| 92 | +In your configuration file, you can specify paths and controllers to be scanned for FastAPI attributes. |
| 93 | + |
| 94 | +```php |
| 95 | +return [ |
| 96 | + //Paths to check for controllers that use fast-api |
| 97 | + 'paths' => [ |
| 98 | + app_path('Http/Controllers'), |
| 99 | + ], |
| 100 | + |
| 101 | + //Specify controllers that are not included in the paths |
| 102 | + 'controllers' => [ |
| 103 | + \App\Http\Controllers\CustomController::class |
| 104 | + ], |
| 105 | +]; |
| 106 | +``` |
| 107 | + |
| 108 | +### Artisan Commands |
| 109 | + |
| 110 | +FastAPI provides useful commands for caching and clearing controllers: |
| 111 | + |
| 112 | +- Cache the controllers for better scanning performance: |
| 113 | + |
| 114 | + ```bash |
| 115 | + php artisan fast-api:cache |
| 116 | + ``` |
| 117 | + |
| 118 | +- Clear the cached controllers: |
| 119 | + |
| 120 | + ```bash |
| 121 | + php artisan fast-api:clear-cache |
| 122 | + ``` |
| 123 | + |
| 124 | +### Performance |
| 125 | + |
| 126 | +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. |
| 127 | + |
| 128 | +```bash |
| 129 | +php artisan route:cache |
| 130 | +``` |
| 131 | + |
| 132 | +## Contributing |
| 133 | + |
| 134 | +Feel free to submit issues and pull requests. Contributions are welcome! |
| 135 | + |
| 136 | +## License |
| 137 | + |
| 138 | +The MIT License (MIT). Please see [License File](LICENSE) for more information. |
| 139 | + |
| 140 | +--- |
| 141 | + |
| 142 | +This `README.md` gives a clear overview of your package's purpose, usage, features, and installation steps, ensuring potential users and contributors understand it quickly. |
0 commit comments