Compare commits

...

3 Commits

Author SHA1 Message Date
c7e113e325 rest api expand, url params 2024-10-07 11:54:10 +03:00
04da662275 Merge branch 'master' of https://git.itguild.info/stasbilay02/MicroFrameWork 2024-10-07 11:31:20 +03:00
5fe6bd9629 rest api expand 2024-10-07 11:31:16 +03:00
4 changed files with 19 additions and 27 deletions

View File

@ -119,11 +119,11 @@ class Request
/**
* Возвращает GET - параметр.
* @param string $param Параметр.
* @param mixed $defaultValue Значение если, параметр не передан.
* @param string|null $param Параметр.
* @param mixed|null $defaultValue Значение если, параметр не передан.
* @return mixed
*/
public function get($param = null, $defaultValue = null): mixed
public function get(string $param = null, mixed $defaultValue = null): mixed
{
if (is_null($param)) {
return $_GET;

View File

@ -11,6 +11,12 @@ class RestController
protected Model $model;
protected function expand(): array
{
return [];
}
#[NoReturn] public function actionIndex(): void
{
$request = new Request();
@ -29,7 +35,14 @@ class RestController
#[NoReturn] public function actionView($id): void
{
$expand = $this->expand();
$request = new Request();
$expandParams = explode( ",", $request->get('expand') ?? "");
$model = $this->model->where("id", $id)->first();
$finalExpand = array_intersect($expandParams, $expand);
if ($finalExpand){
$model->load($finalExpand);
}
$res = [];
if ($model){
$res = $model->toArray();

View File

@ -14,30 +14,9 @@ class PostRestController extends RestController
$this->model = new Post();
}
#[NoReturn] public function actionView($id): void
protected function expand(): array
{
$model = $this->model->where("id", $id)->first();
$model->load("user");
$res = [];
if ($model){
$res = $model->toArray();
}
$this->renderApi($res);
return ["user"];
}
#[NoReturn] public function actionDelete($id): void
{
$model = $this->model->where("id", $id)->first();
$model->load("user");
$res = [];
if ($model){
$res = $model->toArray();
}
$model->delete();
$this->renderApi($res);
}
}

View File

@ -30,7 +30,7 @@ class Post extends Model
];
}
public function user()
public function user(): \Illuminate\Database\Eloquent\Relations\BelongsTo
{
return $this->belongsTo(User::class);
}