Download the PHP package hyder/json-response without Composer
On this page you can find all versions of the php package hyder/json-response. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download hyder/json-response
More information about hyder/json-response
Files in hyder/json-response
Package json-response
Short Description Json response handling for HTTP request.
License MIT
Informations about the package json-response
JSON-Response Laravel Package
Overview
The JSON-Response package provides a simple and minimalistic interface for handling JSON responses in your Laravel applications. It offers convenient methods for returning success responses and error responses with standardized formats.
Installing
You can install the JSON-Response package via Composer. Run the following command in your terminal:
Optional
The service provider will automatically get registered. Or you may manually add the service provider in your config/app.php
file:
Uses
Basic Usage
To use the JSON-Response package, you need to import the JsonResponse facade and make use of its methods in your controllers or routes.
Available Success Methods
The following methods are available for returning success responses:
created($message = "Data created successfully!", $data = null)
Returns a response with a 201 Created status code, a success message, and optional data.updated($message = "Data updated successfully!", $data = null)
Returns a response with a 200 OK status code, a success message, and optional data.success($message = "Request completed successfully!")
Returns a response with a 200 OK status code and a success message.withData($data, $message = "Data fetched successfully!")
Returns a response with a 200 OK status code, a success message, and the provided data.
Method Chaining
You can chain methods together for more flexibility:
Please note that the statusCode()
method will not affect the chaining when using the created()
method.
Success Response
Available Error Methods
The following methods are available for returning error responses:
-
badRequest($message = "Bad request!")
Returns a response with a 400 Bad Request status code and an optional error message. This method is used to indicate a general "bad request" error. -
unauthenticated($message = 'Unauthenticated!')
Returns a response with a 401 Unauthorized status code and an optional error message. It is used to indicate that the user making the request is not authenticated. -
invalidRequest($message = 'Invalid request!')
Returns a response with a 403 Forbidden status code and an optional error message. It is used to indicate that the request is invalid or does not meet the server's expectations. -
validationError($message = 'Required field is missing!')
Returns a response with a 422 Unprocessable Entity status code and an optional error message. It is used to indicate that the request data failed validation checks. -
notFound($message = 'Not found!')
Returns a response with a 404 Not Found status code and an optional error message. It is used to indicate that the requested resource was not found. -
internalError($message = 'Internal server error!')
Returns a response with a 500 Internal Server Error status code and an optional error message. It is used to indicate that an unexpected internal server error occurred. error($message = 'Something went wrong!', int $statusCode = 500)
Returns a response with the specified status code and error message.
Please note that you can provide a custom error message for each error response if needed.
Feel free to adjust the method names and default error messages to better suit your application's needs.
Chaining with error method
You can chain methods together for more flexibility:
Please note that the statusCode()
method will affect only when chaining with error()
method.
Response
Customized Response
In addition to the provided methods, you can also return a customized JSON response using the response()
method. This method allows you to specify the data, status code, and headers for the response.
License
This package is open-source software licensed under the MIT license.
Credits
This package is developed and maintained by Tofayel Hyder Abhi.