PHP code example of dantepiazza / laravel-api-response
1. Go to this page and download the library: Download dantepiazza/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/ */
dantepiazza / laravel-api-response example snippets
// 1. Helper function
api()->success()->response();
// 2. Facade
use DantePiazza\LaravelApiResponse\Facades\ApiResponse;
ApiResponse::success()->response();
// 3. Dependency injection (recommended for testability)
use DantePiazza\LaravelApiResponse\ApiResponse;
class UserController extends Controller
{
public function __construct(private ApiResponse $api) {}
public function index(): JsonResponse
{
return $this->api->success($users, 'Users retrieved')->response();
}
}
// app/Http/Controllers/Controller.php
abstract class Controller
{
public function __construct(protected ApiResponse $api) {}
}
use DantePiazza\LaravelApiResponse\ApiResponse;
ApiResponse::macro('teapot', function () {
return $this->set(ApiResponse::STATUS_FAIL, 418, "I'm a teapot");
});
// Then anywhere:
api()->teapot()->response();