2017 © Pedro Peláez
 

library makeresponse

Make Response Schema Responder.

image

farhanwazir/makeresponse

Make Response Schema Responder.

  • Thursday, April 6, 2017
  • by farhanwazir
  • Repository
  • 1 Watchers
  • 2 Stars
  • 10 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 2 Forks
  • 0 Open issues
  • 8 Versions
  • 0 % Grown

The README.md

Make Response

This PHP library helps to generate global standard response in JSON and Array for client (API Client, POS Client, Web Client etc). It is not framework dependent, you are open to use it with any PHP project., (*1)

Why to use this library

  • This library is a standard formatted schema to response, you don't need to make a document for every web service, just refer to this.
  • This library works logically, you must respect standards.
  • It is framework free and if you want it to use with any framework then you can. It has Laravel & Lumen build-in support as described below.

Installation

Via composer command, (*2)

composer require farhanwazir/makeresponse

Or add below line in your composer.json and run composer update command ```composer.json "farhanwazir/makeresponse" : "1.*", (*3)


## Laravel & Lumen Configuration ### Laravel user's For Laravel user's can add below line in config/app.php ```php 'providers' => [ ... FarhanWazir\MakeResponse\MakeResponseServiceProvider::class ... ]

Do not close config/app, go down inside "aliases", (*4)

'aliases' => [
    ...
    'MakeResponse' => FarhanWazir\MakeResponse\Facade\MakeResponse::class
    ...
]

Lumen user's

For Lumen user's can add below line in bootstrap/app.php, (*5)

$app->register(FarhanWazir\MakeResponse\MakeResponseServiceProvider::class);

Add alias as facade in laravel, (*6)

if (!class_exists('MakeResponse')) {
    class_alias(FarhanWazir\MakeResponse\Facade\MakeResponse::class, 'MakeResponse');
}

Usage

You just need to call makeResponse() helper function to respond to request. Below listed available methods will help you to explorer MakeResponse., (*7)

  • setStatus(numeric) set numeric value with positive or negative
  • setMessage(string) set string message not big string, it is for short message
  • setErrors(sting|array) set string or array for errors
  • setResult(string|array) set string or array for result parameter of your response
  • set(status, result, errors, message) set method for set response parameters once, (*8)

  • get() get formatted response collection, (*9)

  • get()->toArray() convert collection in array
  • get()->toJson() convert collection in json

Use via helper function

makeResponse($status, $result, $errors, $message, $array);
  • $status must be numeric positive or negative.
  • $result string or array. Default null
  • $errors string or array. Default null
  • $message one line string. Default null
  • $array boolean, it is belongs to returning response (in array or json). Default true

Get response example

Example 1: Get formatted response in array, (*10)

makeResponse(1, ['id' => 1, 'name' => 'Farhan Wazir']);

/** Output
[
    'status' => 1,
    'result' => ['id' => 1, 'name' => 'Farhan Wazir']
]
*/

Example 2: Get formatted response in json, (*11)

makeResponse()->setStatus(0)->setErrors('You provided input is wrong.')->get();
/** Output
{
    'status' : 0,
    'errors' : ['You provided input is wrong.']
}
*/

//OR
makeResponse(1, ['id' => 1, 'name' => 'Farhan Wazir'], null, null, false);
/** Output
{
    'status' : 1,
    'errors' : ['id' => 1, 'name' => 'Farhan Wazir']
}
*/

Example 3: Verify formatted response to client, (*12)

//Response will be in array
makeResponse(1);
/** Output
[
    'status' => 1
]
*/

//Response will be in json
makeResponse(1, null, null, null, false);
/** Output
{
    'status' : 1
}
*/

Example 4: Convert response in array, (*13)

// makeResponse()->toArray() converts into array and same for json by toJson()
makeResponse()->setStatus(0)->setErrors('You provided input is wrong.')->get()->toArray();

Example 5: Direct class call approach, (*14)

$response = new FarhanWazir\MakeResponse\Response();
$response->setStatus(1)->setMessage('Make you feel comfortable');
$response->setResult( array('id' => 1, 'name' => 'Make Responder') );


print $response->get(); //or $response->get()->toJson();
/** Output
{
    'status' : 1,
    'message' : 'Make you feel comfortable',
    'result' : ['id' => 1, 'name' => 'Make Responder']
}
*/

//convert response in array
print_r($response->get()->toArray());
[
    'status' => 1,
    'message' => 'Make you feel comfortable',
    'result' => ['id' => 1, 'name' => 'Make Responder']
]

Laravel and Lumen user's, (*15)

You can also use above illustrated example and below is for Laravel and Lumen style:, (*16)

//Response will be in json
MakeResponse::set(1, ['id' => 1, 'name' => 'Farhan Wazir'])->get();

//Response will be in array
MakeResponse::set(0, null, 'Your input is wrong', 'Input error')->get()->toArray();

Thank you for using

The Versions

06/04 2017

dev-master

9999999-dev

Make Response Schema Responder.

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

06/04 2017

v1.5.4

1.5.4.0

Make Response Schema Responder.

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

02/04 2017

v1.5.3

1.5.3.0

Make Response Schema Responder.

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

02/04 2017

v1.5.2

1.5.2.0

Make Response Schema Responder.

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

02/04 2017

v1.5

1.5.0.0

Make Response Schema Responder.

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

01/04 2017

v1.1.1

1.1.1.0

Make Response Schema Responder.

  Sources   Download

MIT

The Requires

  • php >=5.4

 

01/04 2017

v1.1

1.1.0.0

Make Response Schema Responder.

  Sources   Download

MIT

The Requires

  • php >=5.4

 

01/04 2017

v1.0

1.0.0.0

Make Response Schema Responder.

  Sources   Download

MIT

The Requires

  • php >=5.4