PHP code example of farhanwazir / makeresponse

1. Go to this page and download the library: Download farhanwazir/makeresponse library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

farhanwazir / makeresponse example snippets


'providers' => [
    ...
    FarhanWazir\MakeResponse\MakeResponseServiceProvider::class
    ...
]

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

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

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

makeResponse($status, $result, $errors, $message, $array);

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

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

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']
}
*/

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

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

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

$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']
]

//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();