Laravel

Kit: An Opinionated API Starter Kit for Laravel

  • Leverage Kit for a structured approach to building versioned JSON APIs in Laravel.
  • Utilize built-in security features to enhance the safety of your API endpoints.
  • Implement Sunset middleware to manage API endpoint deprecation effectively.
  • Streamline your development process with integrated testing and CI/CD tools.

In the rapidly evolving landscape of web development, having a robust starting point for building APIs is crucial. Kit offers a comprehensive solution for developers using Laravel, providing essential features for creating secure, scalable, and maintainable APIs. This starter kit is designed to save time and effort by offering a structured framework that adheres to best practices.

With Kit, developers can focus on building their applications rather than worrying about the underlying architecture. The kit emphasizes security, versioning, and documentation, making it a valuable resource for both new and experienced Laravel developers looking to streamline their API development process.

Continue Reading

Getting Started with Kit

To begin using Kit, developers need to clone the repository, install the necessary dependencies, and run the setup script. The following commands will set up your environment:

composer install
composer run setup
php artisan serve

The composer run setup command performs several critical tasks:

  • Copies .env.example to .env.
  • Generates a unique application key.
  • Runs migrations against a local SQLite database.

API Architecture Overview

Kit adopts specific architectural choices to streamline API development:

  • No global /api prefix: Routes are versioned directly, such as /v1/auth/login.
  • Invokable controllers: Each controller is designed as a single __invoke method.
  • Form Requests for validation: Payloads are validated using dedicated FormRequest classes, ensuring clean and maintainable code.
  • JSON:API resource format: Responses are structured consistently to enhance the client experience.

Authentication Mechanism

Kit simplifies the authentication process with nine dedicated routes that handle the complete authentication lifecycle:

  • Registration and Login: Both processes return tokens through Laravel Sanctum.
  • User Information: The route /v1/auth/me retrieves user data and requires a Bearer token.
  • Email Verification: Utilizes signed URLs for secure email verification.
  • Password Reset: Implements anti-enumeration responses to enhance security.

Security Features

Security is a top priority in Kit, which includes several built-in features:

  • ULID Primary Keys: User records are assigned ULID primary keys to enhance uniqueness and security.
  • Predefined Rate Limits: Configurable rate limits on authentication endpoints help prevent abuse.
  • Mandatory Content-Type Enforcement: Ensures that all write requests have the application/json content type.
  • Hardened Response Headers: Includes security headers like X-Content-Type-Options: nosniff and X-Frame-Options: DENY.
  • Request ID Tracking: Facilitates tracking of requests for better debugging and logging.
  • Audit Logging: Sensitive operations are logged for security auditing.

Sunset Middleware

To manage the deprecation of API endpoints over time, Kit includes a Sunset middleware. This middleware can be applied directly to routes with the following parameters:

Route::middleware('sunset:2027-01-01,https://api.acme.com/v2/auth/login,true')
    ->post('/v1/auth/login', LoginController::class);

While the endpoint is active, the middleware appends Deprecation, Sunset, and Link headers to every response, allowing API clients to prepare for changes. Once the sunset date is reached, and enforcement is enabled, the endpoint will return a 410 Gone status.

Documentation Generation

Kit simplifies API documentation with Scribe, generating documentation that adheres to the OpenAPI specification. The documentation setup utilizes attribute-based annotations instead of traditional docblock annotations, streamlining the documentation process.

Localization Support

To cater to a diverse user base, Kit respects the Accept-Language request header and responds with a Content-Language header. The default supported locales are English and Spanish, with translation files located at lang/en/api.php and lang/es/api.php.

Development Tooling

Kit incorporates several tools to enhance the development experience:

  • Pest: A testing framework that simplifies the process of writing tests.
  • PHPStan: A static analysis tool that helps identify potential issues in the codebase.
  • Pint: A code formatting tool that ensures consistent code style.
  • Rector: An automated refactoring tool that helps maintain code quality.
  • GitHub Actions: CI workflows run tests on every push, apply daily dependency updates, and conduct security scans using composer audit and Gitleaks.

Requirements

To effectively use Kit, developers should ensure their environment meets the following requirements:

  • PHP Version: 8.5 or higher.
  • Laravel Version: 12 or higher.
  • Database: SQLite for local development, with the option to configure other databases.

Conclusion

Kit serves as a powerful starter kit for developers looking to build APIs using Laravel. By providing a structured approach that emphasizes security, versioning, and comprehensive documentation, Kit allows developers to focus on delivering high-quality applications. The integration of modern development tools and practices further enhances the development experience, making it an invaluable resource for both new and seasoned Laravel developers.

Frequently Asked Questions

What is Kit for Laravel?

Kit is an opinionated API starter kit designed for Laravel that provides a structured framework for building secure and maintainable versioned JSON APIs.

How does Kit enhance API security?

Kit includes built-in security features such as ULID primary keys, predefined rate limits, mandatory content type enforcement, and hardened response headers to protect API endpoints.

Can I manage API endpoint deprecation with Kit?

Yes, Kit includes Sunset middleware that allows developers to manage the deprecation of API endpoints effectively by specifying sunset dates and successor URLs.

Call To Action

Explore Kit today to streamline your Laravel API development and leverage its powerful features for building secure and maintainable applications.

Note: Provide a strategic conclusion reinforcing long-term business impact and keyword relevance.

Disclaimer: Tech Nxt provides news and information for general awareness purposes only. While we strive for accuracy, we do not guarantee the completeness or reliability of any content. Opinions expressed are those of the authors and not necessarily of Tech Nxt. We are not liable for any actions taken based on the information published. Content may be updated or changed without prior notice.