|
1 | | -# Laravel Response Builder |
| 1 | +# Laravel API Response Builder |
2 | 2 |
|
3 | | -A Laravel package designed to simplify the creation of structured and formatted API responses (JSON, XML) with custom status codes, messages, and data. |
| 3 | +**Laravel API Response Builder** is a Laravel package designed to simplify the creation of structured and formatted API responses (JSON, XML) with customizable status codes, messages, and data. This package provides a flexible way to handle API responses, including error handling, logging, and various configuration options. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- **Structured Responses**: Define custom response structures for consistency. |
| 8 | +- **Format Options**: Choose between JSON and XML response formats. |
| 9 | +- **Logging**: Log requests and responses for debugging and auditing. |
| 10 | +- **Compression**: Optionally compress responses to reduce payload size. |
| 11 | +- **Error Handling**: Include detailed error messages for debugging. |
4 | 12 |
|
5 | 13 | ## Installation |
6 | 14 |
|
7 | | -Install via Composer: |
| 15 | +To install the package, run the following command: |
| 16 | + |
| 17 | +```bash |
| 18 | +composer require doliveira/laravel-api-response-builder |
| 19 | +``` |
| 20 | + |
| 21 | +## Configuration |
| 22 | + |
| 23 | +After installing the package, publish the configuration file using the following command: |
8 | 24 |
|
9 | 25 | ```bash |
10 | | -composer require d-oliveira/laravel-response-builder |
| 26 | +php artisan vendor:publish --provider="Doliveira\LaravelResponseBuilder\Providers\ResponseBuilderServiceProvider" |
| 27 | +``` |
| 28 | + |
| 29 | +This will create a `config/laravel-api-response-builder.php` file where you can customize the package settings. |
| 30 | + |
| 31 | +## Configuration Options |
| 32 | + |
| 33 | +Here's a brief overview of the available configuration options: |
| 34 | + |
| 35 | +- **`default_format`**: The default format for responses (`'json'` or `'xml'`). |
| 36 | +- **`json_options`**: Options for JSON encoding (e.g., `JSON_UNESCAPED_SLASHES`). |
| 37 | +- **`xml_root_element`**: The default root element for XML responses. |
| 38 | +- **`default_status_code`**: The default HTTP status code for responses. |
| 39 | +- **`detailed_errors`**: Whether to include detailed error messages for debugging. |
| 40 | +- **`wrap_data`**: Whether to wrap response data in an additional `'data'` key. |
| 41 | +- **`default_locale`**: The default locale for custom messages. |
| 42 | +- **`default_response_language`**: The default language for responses (`'en'` or `'pt'`). |
| 43 | +- **`compress_responses`**: Whether to compress responses. |
| 44 | +- **`log_responses`**: Whether to log responses. |
| 45 | +- **`logging_level`**: The level of detail for logging responses. |
| 46 | +- **`api_key_header`**: The name of the header used for API keys. |
| 47 | +- **`custom_response_structure`**: Define a custom structure for responses. |
| 48 | +- **`debug_mode`**: Enable or disable debug mode. |
| 49 | +- **`log_requests`**: Whether to log requests. |
| 50 | +- **`log_response_time`**: Whether to log response times. |
| 51 | + |
| 52 | +## Usage |
| 53 | + |
| 54 | +### JSON Responses |
| 55 | + |
| 56 | +#### Success Response |
| 57 | + |
| 58 | +```php |
| 59 | +use Doliveira\LaravelResponseBuilder\JsonResponse; |
| 60 | + |
| 61 | +return JsonResponse::success($data, 'Operation successful.'); |
| 62 | +``` |
| 63 | + |
| 64 | +#### Error Response |
| 65 | + |
| 66 | +```php |
| 67 | +use Doliveira\LaravelResponseBuilder\JsonResponse; |
| 68 | + |
| 69 | +return JsonResponse::error(400, 'Bad request.', $data); |
11 | 70 | ``` |
| 71 | + |
| 72 | +### XML Responses |
| 73 | + |
| 74 | +To create XML responses, use the `XmlResponse` class: |
| 75 | + |
| 76 | +```php |
| 77 | +use Doliveira\LaravelResponseBuilder\XmlResponse; |
| 78 | + |
| 79 | +$response = new XmlResponse($statusCode, $message, $data); |
| 80 | +return $response->build(); |
| 81 | +``` |
| 82 | + |
| 83 | +## Error Handling |
| 84 | + |
| 85 | +The package allows you to include detailed error messages for debugging. By default, this is disabled in production environments: |
| 86 | + |
| 87 | +```php |
| 88 | +'logging_level' => 'info', |
| 89 | +'detailed_errors' => false, |
| 90 | +``` |
| 91 | + |
| 92 | +You can enable detailed error messages for debugging purposes by setting `'detailed_errors'` to `true` in the configuration. |
| 93 | + |
| 94 | +## Examples |
| 95 | + |
| 96 | +### Success JSON Response |
| 97 | + |
| 98 | +```json |
| 99 | +{ |
| 100 | + "status": 200, |
| 101 | + "message": "Operation successful.", |
| 102 | + "data": { |
| 103 | + "id": 1, |
| 104 | + "name": "Example" |
| 105 | + } |
| 106 | +} |
| 107 | +``` |
| 108 | + |
| 109 | +### Error JSON Response |
| 110 | + |
| 111 | +```json |
| 112 | +{ |
| 113 | + "status": 400, |
| 114 | + "message": "Bad request.", |
| 115 | + "data": null |
| 116 | +} |
| 117 | +``` |
| 118 | + |
| 119 | +### Success XML Response |
| 120 | + |
| 121 | +```xml |
| 122 | +<response> |
| 123 | + <status>200</status> |
| 124 | + <message>Operation successful.</message> |
| 125 | + <data> |
| 126 | + <id>1</id> |
| 127 | + <name>Example</name> |
| 128 | + </data> |
| 129 | +</response> |
| 130 | +``` |
| 131 | + |
| 132 | +### Error XML Response |
| 133 | + |
| 134 | +```xml |
| 135 | +<response> |
| 136 | + <status>400</status> |
| 137 | + <message>Bad request.</message> |
| 138 | + <data/> |
| 139 | +</response> |
| 140 | +``` |
| 141 | + |
| 142 | +## Logging |
| 143 | + |
| 144 | +The package can log requests, responses, and response times. Configure logging settings in `config/laravel-api-response-builder.php`: |
| 145 | + |
| 146 | +```php |
| 147 | +'log_responses' => true, |
| 148 | +'log_requests' => true, |
| 149 | +'log_response_time' => true, |
| 150 | +'logging_level' => 'info', |
| 151 | +``` |
| 152 | + |
| 153 | +## Contributing |
| 154 | + |
| 155 | +If you want to contribute to the development of this package, please fork the repository and submit a pull request. |
| 156 | + |
| 157 | +## License |
| 158 | + |
| 159 | +This package is licensed under the MIT License. See the [LICENSE](LICENSE) file for more details. |
| 160 | + |
| 161 | +## Contact |
| 162 | + |
| 163 | +For any questions or feedback, please contact: |
| 164 | + |
| 165 | +- **Danilo Oliveira**: [daniloworkdev@gmail.com](mailto:daniloworkdev@gmail.com) |
| 166 | +- **Website**: [daniloo.dev](http://www.daniloo.dev) |
0 commit comments