再次优化
This commit is contained in:
@@ -60,9 +60,9 @@ abstract class AbstractModel extends AetherModel
|
||||
* @param array $columns
|
||||
* @return AbstractModel
|
||||
*/
|
||||
public static function findOrFail(int $id, $columns = []): self
|
||||
public static function findOrFail(mixed $id, $columns = []): self
|
||||
{
|
||||
$model = self::find($id);
|
||||
$model = self::find($id, $columns);
|
||||
if (!$model) {
|
||||
throw new ModelNotFoundException();
|
||||
}
|
||||
@@ -74,10 +74,7 @@ abstract class AbstractModel extends AetherModel
|
||||
*/
|
||||
public static function createOne(array $data): self
|
||||
{
|
||||
$model = new static();
|
||||
$model->fill($data);
|
||||
$model->save();
|
||||
return $model;
|
||||
return static::query()->create($data);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -43,7 +43,7 @@ abstract class AetherController
|
||||
/**
|
||||
* 创建资源 (RESTFul: POST /resources)
|
||||
*/
|
||||
public function store()
|
||||
public function store(): array
|
||||
{
|
||||
$data = $this->request->all();
|
||||
$id = $this->getService()->create($data);
|
||||
@@ -71,8 +71,8 @@ abstract class AetherController
|
||||
|
||||
/**
|
||||
* 获取对应的服务类
|
||||
* @return \Aether\AbstractService
|
||||
* @return AbstractService
|
||||
*/
|
||||
abstract protected function getService();
|
||||
abstract protected function getService(): AbstractService;
|
||||
|
||||
}
|
||||
|
||||
@@ -4,14 +4,21 @@ declare(strict_types=1);
|
||||
|
||||
namespace Aether;
|
||||
|
||||
use Hyperf\Context\ApplicationContext;
|
||||
use Hyperf\ExceptionHandler\ExceptionHandler;
|
||||
use Hyperf\Rpc\Protocol;
|
||||
use Psr\Container\ContainerExceptionInterface;
|
||||
use Psr\Container\NotFoundExceptionInterface;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Throwable;
|
||||
use Hyperf\Context\Context;
|
||||
|
||||
class RpcExceptionHandler extends ExceptionHandler
|
||||
{
|
||||
/**
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
public function handle(Throwable $throwable, ResponseInterface $response): ResponseInterface
|
||||
{
|
||||
// 微服务间调用返回更精简的错误信息
|
||||
@@ -21,7 +28,8 @@ class RpcExceptionHandler extends ExceptionHandler
|
||||
'request_id' => Context::get('request_id', ''),
|
||||
];
|
||||
|
||||
$protocol = make(Protocol::class);
|
||||
$protocol = ApplicationContext::getContainer()->get(Protocol::class);
|
||||
// $response->getBody()->write($protocol->pack($data));
|
||||
$response->getBody()->write($protocol->pack($data));
|
||||
|
||||
return $response;
|
||||
|
||||
Reference in New Issue
Block a user