mkd/laravel-fast-api

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

v0.0.1 2024-10-16 19:46 UTC

This package is auto-updated.

Last update: 2024-10-16 19:50:36 UTC


README

Latest Version Issues 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:

composer require mkd/laravel-fast-api

Usage

Define FastAPI Routes

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.

Supported Methods and Functions

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',
];

Configurations

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
    ],
];

Artisan Commands

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

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.

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 for more information.