PHP code example of zerkxubas / api-response-laravel

1. Go to this page and download the library: Download zerkxubas/api-response-laravel 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/ */

    

zerkxubas / api-response-laravel example snippets


use Zerkxubas\ApiResponseLaravel\StatusCode;

$status = StatusCode::OK;

// use this namespace.
use Zerkxubas\ApiResponseLaravel\Http\Middleware\RequiresJsonMiddleware;

// Add this line inside ->withMiddleware(){}
$middleware->api()->append(RequiresJsonMiddleware::class);


use Zerkxubas\ApiResponseLaravel\Http\Controllers\ApiResponseController;

class MyCustomController extends ApiResponseController
{
    // send response
    return $this->sendResponse($data, $message, $code)

    // send errorResponse
    return $this->sendError($error, $errorMessages, $code);
}


// Example of sending a success response
$data = ['key' => 'value'];
return $this->sendResponse('Data retrieved successfully.', $data, 200);

// Or
return $this->sendResponse('Data retrieved successfully.', $data, StatusCode::OK);


// Example of sending an error response
$errorMessages = [
    'error_detail' => 'Customized errors in bulk.'
];
return $this->sendError('File not found.', $errorMessages, 404);

// Or
return $this->sendError('File not found.', $errorMessages, StatusCode::NOT_FOUND);




namespace App\Services;

use Zerkxubas\ApiResponseLaravel\Contracts\ApiResponseInterface;

class CustomApiResponse implements ApiResponseInterface
{
    public function sendResponse($message, $data = [], $code = 200)
    {
        // Your custom response structure
    }

    public function sendError($message, $data = [], $code = 400)
    {
        // Your custom error structure
    }
}


return [    
    'implementation' => App\Services\CustomApiResponse::class,
];
bash
php artisan vendor:publish --tag="apiresponse"
StatusCode::SERVER_ERROR