1. Go to this page and download the library: Download goletter/hyperf-resource 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/ */
goletter / hyperf-resource example snippets
use Goletter\Resource\ApiResponse;
class UserService
{
use ApiResponse;
public function show(int $id)
{
$user = User::query()->findOrFail($id);
// 自动解析 App\Resource\User 或 App\Resource\UserResource
return $this->success($user);
// 或显式指定
// return $this->success($user, UserResource::class, 200, 'ok');
}
public function index()
{
$users = User::query()->paginate();
return $this->collection($users, UserResource::class);
}
public function store()
{
// 业务失败:抛出 BusinessException(需在项目中注册异常处理器)
$this->fail(422, '邮箱已被占用');
}
}