PHP code example of milutinvelisic / response-helper

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

    

milutinvelisic / response-helper example snippets



namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use MilutinVelisic\ResponseHelper\ResponseHelper;

class ExampleController extends Controller
{
    public function __construct(protected readonly ResponseHelper $responseHelper) {}
    
    public function index()
    {
        $data = ['message' => 'Data retrieved successfully', 'items' => [1, 2, 3]];
        return $this->responseHelper->success($data, 'Successfully fetched data');
    }

    public function noDataSuccess()
    {
        return $this->responseHelper->success(null, 'Operation completed successfully');
    }

    public function customStatusCodeSuccess()
    {
        return $this->responseHelper->success(['id' => 1], 'Resource created', 201);
    }


namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use MilutinVelisic\ResponseHelper\ResponseHelper;
use Exception;

class ErrorController extends Controller
{
    public function __construct(protected readonly ResponseHelper $responseHelper) {}

    public function exampleError()
    {
        try {
            // Some code that might throw an exception
            throw new Exception('Something went wrong during processing.');
        } catch (Exception $e) {
            return $this->responseHelper->error($e, 'Failed to process request', 500);
        }
    }

    public function validationError()
    {
        $exception = new \Illuminate\Validation\ValidationException(\Illuminate\Support\Facades\Validator::make([], [
            'name' => '