PHP code example of drdre4life2 / laravel-api-response

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

    

drdre4life2 / laravel-api-response example snippets


namespace App\Http\Controllers;

use Drdre4life2\ApiResponse\Traits\HasApiResponse;


abstract class Controller
{
    use HasApiResponse;

}

return $this->okResponse('Success message', ['key' => 'value']);
return $this->createdResponse('Resource created', ['id' => 1]);
return $this->noContentResponse();

return $this->badRequestResponse('Invalid request', ['error' => 'Details']);
return $this->unauthenticatedResponse('Unauthorized');
return $this->forbiddenResponse('Access denied');
return $this->notFoundResponse('Resource not found');

return $this->serverErrorResponse('Server error occurred');

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class ExampleController extends Controller
{
    public function index(Request $request)
    {
        $data = ['message' => 'Hello, World!'];
        return $this->okResponse('Data retrieved successfully', $data);
    }
}
bash
php artisan test