Download the PHP package fenox/laravel-api-json without Composer
On this page you can find all versions of the php package fenox/laravel-api-json. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download fenox/laravel-api-json
More information about fenox/laravel-api-json
Files in fenox/laravel-api-json
Package laravel-api-json
Short Description A Laravel package that simplifies API development by automating CRUD operations, ensures consistent JSON error handling, and streamlines the creation of essential components for RESTful APIs.
License MIT
Informations about the package laravel-api-json
JSON API Base Package for Laravel
Table of Contents
- Description
- Requirements
- Installation
- Usage
- Auth
- Error Handling
- Advanced Configuration
- Contributing
- License
Description
The API Base Package for Laravel is designed to streamline API development by automating the creation of essential components. It facilitates the rapid generation of models, migrations, controllers, requests, policies, seeders, factories, and tests, significantly reducing the amount of boilerplate code developers need to write.
This package ensures that all API responses are consistently returned in JSON format, providing a uniform approach to error handling and data interaction across your application. With built-in validation and error handling, the package improves developer efficiency while maintaining best practices in API development.
Key features include:
- Automated generation of CRUD components for any model.
- Consistent JSON responses for error handling.
- Customizable routes and validation rules tailored to your application's needs.
The package is perfect for developers looking to set up a robust API quickly and efficiently while maintaining flexibility for future enhancements.
Requirements
To use the API Base Package for Laravel, ensure that your development environment meets the following requirements:
- PHP: ^8.3
- Laravel: ^11.0
- Composer: Ensure you have Composer installed and properly configured.
Recommended Environment
- A local development environment such as Laravel Valet or Homestead is recommended for seamless integration and testing.
- Familiarity with Laravel and RESTful API development practices will help you make the most out of this package.
Installation
To get started with the API Base Package for Laravel, follow these steps:
-
Create a new Laravel project (if you haven't already):
-
Navigate to your project directory:
-
Install the API Base Package: Use Composer to require the package:
-
Publish the authentication resources: After installing the package, publish the authentication-related resources:
This will create necessary request classes and controllers to handle authentication, which you can customize as needed.
- Run the API routes installation command (if applicable): To set up your API routes, run:
Usage
The API Base Package simplifies the process of creating and managing API endpoints. To create a new API model along with its associated components, use the make:apimodel
command.
Example: Creating a Category API
-
Run the command: To generate the Category API, execute the following command:
-
Modify the Migration File: After generating the model and migration, navigate to the migration file in to add the necessary fields for your database schema. For instance, if you're adding and fields, your migration might look like this:
-
Add `` Fields to the Model: In your model file located at , ensure you add the necessary fields to the property to allow mass assignment:
-
Define Validation Rules in Requests: In the generated and files located in , set your validation rules. If a field is optional, use or :
-
Setup Routes: Ensure your API routes are configured properly in . The generated routes will typically look like this:
By following these steps, you can quickly set up a functional CRUD API for the Category model, ready for further customization and use.
Remember use:
& run:
Auth
his package integrates Laravel Sanctum to provide simple and robust API token authentication for your application.
Steps to Setup Authentication
-
Publish the Auth Requests: After installing the package, you can publish the authentication-related requests by running:
-
Modify the User Model: Ensure that your model (located at ) uses the trait. This allows the model to generate API tokens for authentication:
- Setup Routes:The package will generate the necessary authentication routes automatically. You should add the following routes to your file to enable login and logout functionality:
Error Handling
This package includes built-in error handling to ensure that all exceptions and validation errors are consistently returned in JSON format. This approach improves the uniformity of API responses and simplifies client-side error management.
Common Error Responses
The following are the common error responses that the package handles:
-
Validation Error (422): When a request fails validation, a structured error response is returned. For example:
-
Unauthenticated (401): Returned when a user attempts to access a protected resource without proper authentication.
-
Forbidden (403): Returned when an authenticated user does not have permission to perform a specific action.
-
Resource Not Found (404): Returned when a requested resource or route is not found.
-
Method Not Allowed (405): Returned when the HTTP method used for a request is not allowed for the specified route.
- Server Error (500): Returned for any unexpected server errors.
Advanced Configuration
This package provides a base structure for API development, but it can also be customized or extended depending on your project’s needs.
-
Customizing Controllers
By default, the generated controllers extend the provided by this package. You can customize these controllers by overriding methods like , , , and more to add additional logic or behavior.
Example: Modifying the
-
Adding New Functionality
You are free to extend this package by adding new commands, controllers, or services specific to your API structure.
Example: Adding a Custom Base Controller
You can extend the to create new functionalities shared across your API controllers. For example, you could add new methods or modify existing ones.
Contributing
We would be delighted to receive contributions to improve this package and its functionality. If you find any bugs or have suggestions for new features, please report them by opening an issue on our GitHub repository. Your feedback and contributions are invaluable to us!
License
This package is open-sourced software licensed under the MIT license.
You are free to use, modify, and distribute this package under the terms of the MIT license. For more details, refer to the LICENSE file included in this repository.