PHP code example of hyder / json-response

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

    

hyder / json-response example snippets


'providers' => [
    // ...
    Hyder\JsonResponse\JsonResponseServiceProvider::class,
];

use Hyder\JsonResponse\Facades\JsonResponse;

// ...

public function store(Request $request)
{
    try {

        // Validate the incoming request
        $validator = Validator::make($request->all(), [
            // Validation rules
        ]);

        // Check if validation fails
        if ($validator->fails()) {
            // Return a validation error response
            return JsonResponse::validationError($validator->errors());
        }

        // Process the request and create a new resource

        // Return a success response
        return JsonResponse::created($message);
    } catch(\Exception $ex){
        return JsonResponse::internalError($ex->getMessage());
    }

}

public function update(Request $request, $id)
{
    // Process the request and update the specified resource

    // Return a success response
    return JsonResponse::updated($message);
}


// Return a success response with a custom status code and message
return JsonResponse::statusCode($statusCode)->success('Your message');

// Return a success response with a custom status code, header, and message
return JsonResponse::statusCode($statusCode)->withHeader($header)->success('Your message');

// Return a error response with a custom status code and message
return JsonResponse::statusCode($statusCode)->error($message);

// Return a error response with a custom status code, header, and message
return JsonResponse::withHeader($header)->error($message, 400);

// Return a custom response with a custom status code and data
return JsonResponse::statusCode($statusCode)->response($array);

// Return a custom response with a custom status code, header, and data
return JsonResponse::statusCode($statusCode)->withHeader($header)->response($array);