封装优化
This commit is contained in:
@@ -20,30 +20,59 @@ abstract class AetherController
|
||||
#[Inject]
|
||||
protected ResponseInterface $response;
|
||||
|
||||
|
||||
/**
|
||||
* 获取请求参数.
|
||||
* @param string $key
|
||||
* @param mixed|null $default
|
||||
* @return mixed
|
||||
* 获取资源列表 (RESTFul: GET /resources)
|
||||
*/
|
||||
public function requestParam(string $key, mixed $default = null): mixed
|
||||
public function index(): array
|
||||
{
|
||||
return $this->request->input($key, $default);
|
||||
$params = $this->request->all();
|
||||
$result = $this->getService()->list($params);
|
||||
return AetherResponse::success($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有请求参数.
|
||||
* 获取单个资源 (RESTFul: GET /resources/{id})
|
||||
*/
|
||||
public function requestParams(): array
|
||||
public function show(int $id): array
|
||||
{
|
||||
return $this->request->all();
|
||||
$result = $this->getService()->detail($id);
|
||||
return AetherResponse::success($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回JSON响应.
|
||||
* 创建资源 (RESTFul: POST /resources)
|
||||
*/
|
||||
public function json(array $data): \Psr\Http\Message\ResponseInterface
|
||||
public function store()
|
||||
{
|
||||
return $this->response->json($data);
|
||||
$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 \Aether\AbstractService
|
||||
*/
|
||||
abstract protected function getService();
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user