Files
hyperf_data/extend/Aether/PHP/Hyperf/AetherController.php
阿不叮咚 998f8d8356 再次优化
2025-09-24 15:51:38 +08:00

79 lines
1.8 KiB
PHP

<?php
declare(strict_types=1);
namespace Aether;
use Hyperf\Contract\ContainerInterface;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Contract\ResponseInterface;
abstract class AetherController
{
#[Inject]
protected ContainerInterface $container;
#[Inject]
protected RequestInterface $request;
#[Inject]
protected ResponseInterface $response;
/**
* 获取资源列表 (RESTFul: GET /resources)
*/
public function index(): array
{
$params = $this->request->all();
$result = $this->getService()->list($params);
return AetherResponse::success($result);
}
/**
* 获取单个资源 (RESTFul: GET /resources/{id})
*/
public function show(int $id): array
{
$result = $this->getService()->detail($id);
return AetherResponse::success($result);
}
/**
* 创建资源 (RESTFul: POST /resources)
*/
public function store(): array
{
$data = $this->request->all();
$id = $this->getService()->create($data);
return AetherResponse::success(['id' => $id], '创建成功');
}
/**
* 更新资源 (RESTFul: PUT /resources/{id})
*/
public function update(int $id): array
{
$data = $this->request->all();
$this->getService()->update($id, $data);
return AetherResponse::success(null, '更新成功');
}
/**
* 删除资源 (RESTFul: DELETE /resources/{id})
*/
public function destroy(int $id): array
{
$this->getService()->delete($id);
return AetherResponse::success(null, '删除成功');
}
/**
* 获取对应的服务类
* @return AbstractService
*/
abstract protected function getService(): AbstractService;
}