Download the PHP package milito/response-generator without Composer

On this page you can find all versions of the php package milito/response-generator. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package response-generator

Latest Version on Packagist Total Downloads License

Response Generator

Json Response Generator for Laravel Projects

I use this package to make it easier to change the JSON response structure.

Introduction

Use this package to make more flexible responses. This package can only be used in Laravel.

Usage

First install the package via composer using the following command :

Then import the MilitoResponseGeneratorServiceProvider service provider to config/app.php file :

Now you can use MilitoResponseGenerator facade to generate your responses.

Example

Success Response

This is for success response :

The response should be like this :

Failed Response

This is for failed response :

The response should be like this :

Note 1 : If you don't want to show success, code, or errors fields, you can disable them through the config file.

Note 2 : If you want to change the position of the fields, you can change it through the config file.

Docs

Functions & Classes

Now, how to handle success response. For example, we use facade to start a response, we set response fields step by step. Now we need to know how to work with these functions and how to generate our success response.

Success Response

SuccessState
After using function to start a success response, we will have an object of SuccessState type. We will have the following functions for SuccessState (This is an object) : Function Input Output Description
code(int $code) int status_code MessageState object Sets Http code (what ever you want)
succeeded() void MessageState object Sets Http code as HTTP_OK code (200)
created() void MessageState object Sets Http code as HTTP_CREATED code (201)
accepted() void MessageState object Sets Http code as HTTP_ACCEPTED code (202)
updated() void SendState object Sets http code as HTTP_NO_CONTENT code and doesn't need a body (204)

Note : If used updated() function, it means we need no content response. Because of that, the next state is SendState to generate the response.

MessageState
MessageState has these functions : Function Input Output Description
message(string $message) string message DataState object Sets the message field of response
DataState
DataState has these functions : Function Input Output Description
data(mixed $data) mixed data SendState object Sets the data field of response
send(array $headers = []) array headers JsonResponse object Generates Response.

Note 1 : mixed input type because we pass array or object or json resources or etc... to our data function.

Note 2 : We can send a response with an empty data.

SendState
SendState is the last step. After that, we use send function to generate a response and return it to client. Function Input Output Description
send(array $headers = []) array headers JsonResponse object Generates Response.

Failed Response

FailedState
After using function to start a failed response, we will have an object of FailedState type. We will have the following functions for FailedState (This is an object) : Function Input Output Description
code(int $code) int status_code MessageState object Sets the Http code (what ever you want)
MessageState
MessageState has these functions : Function Input Output Description
message(string $message) string message ErrorState object Set message field of response
MessageState
MessageState has these functions : Function Input Output Description
errors(mixed $errors) mixed message DataState object Sets the error & the errors (as object) fields of response
send(array $headers = []) array headers JsonResponse object Generates Response.

Note 1 : mixed input type because we pass array or object or string or etc... to our errors function.

Note 2 : We can generate response without the error & the errors fields value.

DataState
DataState has these functions : Function Input Output Description
data(mixed $data) mixed data SendState object Sets the data field of response
send(array $headers = []) array headers JsonResponse object Generates Response.

Note 1 : mixed input type because we pass array or object or json resources or etc... to our data function.

Note 2 : We can send a response with an empty data.

Note 3 : Sometimes we need to send data after our request failed. Because of that, we have DataState here.

SendState
SendState is the last step. After that, we use send function to generate a response and return it to client. Function Input Output Description
send(array $headers = []) array headers JsonResponse object Generates Response.

Publish config

Use the following command to publish the config file to change the response fields name or makes some fields hidden from the response :

Test

Use the following command to run tests:

License

MIT


All versions of response-generator with dependencies

PHP Build Version
Package Version
Requires php Version ^7.3|^8.0|^8.1
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package milito/response-generator contains the following files

Loading the files please wait ....