PHP code example of giatechindo / hypervel-response-formatter
1. Go to this page and download the library: Download giatechindo/hypervel-response-formatter 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/ */
giatechindo / hypervel-response-formatter example snippets
// config/autoload/dependencies.php
return [
'dependencies' => [
'invokables' => [
\Giatechindo\HypervelResponseFormatter\ResponseFormatter::class => \Giatechindo\HypervelResponseFormatter\ResponseFormatter::class,
],
],
];
declare(strict_types=1);
namespace App\Http\Controllers;
use Giatechindo\HypervelResponseFormatter\ResponseFormatter;
use Hyperf\HttpServer\Contract\ResponseInterface;
class UserController
{
public function index(ResponseInterface $response)
{
$users = [
['id' => 1, 'name' => 'John'],
['id' => 2, 'name' => 'Jane'],
];
return (new ResponseFormatter($response))->success($users, 'Users retrieved successfully', 200);
}
public function show(ResponseInterface $response)
{
return (new ResponseFormatter($response))->error('User not found', ['id' => 'Invalid ID'], 404);
}
}
return response()->success(
data: ['id' => 1, 'name' => 'John'],
message: 'User retrieved',
statusCode: 200
);
return response()->error(
message: 'Validation failed',
errors: ['email' => 'Invalid format'],
statusCode: 422
);
return response()->success(message: 'Operation completed');
return response()->error(message: 'Not found', statusCode: 404);