rest to menu

This commit is contained in:
Kavalar 2024-10-07 16:13:46 +03:00
parent a3f7a90e39
commit 4c75c9469b
5 changed files with 41 additions and 20 deletions

View File

@ -27,6 +27,15 @@ class CgRouteCollector extends RouteCollector
return $this->addRoute(Route::ANY, $route . '/update/{id}', array_merge($handler, ['actionEdit']), $filters); return $this->addRoute(Route::ANY, $route . '/update/{id}', array_merge($handler, ['actionEdit']), $filters);
} }
public function rest($route, $handler, array $filters = []): CgRouteCollector
{
$this->addRoute(Route::GET, $route, array_merge($handler, ['actionIndex']), $filters);
$this->addRoute(Route::GET, $route . '/{id}', array_merge($handler, ['actionView']), $filters);
$this->addRoute(Route::POST, $route . '/', array_merge($handler, ['actionStore']), $filters);
$this->addRoute(Route::DELETE, $route . '/delete', array_merge($handler, ['actionDelete']), $filters);
return $this->addRoute(Route::ANY, $route . '/update/{id}', array_merge($handler, ['actionEdit']), $filters);
}
public function authorization($route, $handler, array $filters = []) public function authorization($route, $handler, array $filters = [])
{ {
//TODO //TODO

View File

@ -74,17 +74,17 @@ class RestController
} }
#[NoReturn] public function actionCreate(): void #[NoReturn] public function actionStore(): void
{ {
$request = new Request(); $request = new Request();
$data = $request->post(); $data = $request->post();
$data = [ foreach ($this->model->getFillable() as $item){
'title' => 'title' $this->model->{$item} = $data[$item] ?? null;
]; }
Debug::prn($data); $this->model->save();
$model = $this->model;
Debug::prn($model->toArray()); $this->renderApi($this->model->toArray());
$model->save($data);
} }
#[NoReturn] protected function renderApi(array $data): void #[NoReturn] protected function renderApi(array $data): void

View File

@ -0,0 +1,16 @@
<?php
namespace kernel\modules\menu\controllers;
use kernel\modules\menu\models\Menu;
use kernel\RestController;
class MenuRestController extends RestController
{
public function __construct()
{
$this->model = new Menu();
}
}

View File

@ -1,6 +1,7 @@
<?php <?php
use kernel\App; use kernel\App;
use kernel\CgRouteCollector;
use Phroute\Phroute\RouteCollector; use Phroute\Phroute\RouteCollector;
App::$collector->group(["prefix" => "admin"], function (RouteCollector $router) { App::$collector->group(["prefix" => "admin"], function (RouteCollector $router) {
@ -17,3 +18,7 @@ App::$collector->group(["prefix" => "admin"], function (RouteCollector $router)
}); });
}); });
}); });
App::$collector->group(["prefix" => "api"], function (CgRouteCollector $router){
$router->rest("menu", [\kernel\modules\menu\controllers\MenuRestController::class]);
});

View File

@ -1,6 +1,7 @@
<?php <?php
use kernel\App; use kernel\App;
use kernel\CgRouteCollector;
use Phroute\Phroute\RouteCollector; use Phroute\Phroute\RouteCollector;
@ -17,16 +18,6 @@ App::$collector->group(["prefix" => "admin"], function (RouteCollector $router){
}); });
}); });
App::$collector->group(["prefix" => "api"], function (RouteCollector $router){ App::$collector->group(["prefix" => "api"], function (CgRouteCollector $router){
App::$collector->group(["prefix" => "post"], function (RouteCollector $router){ $router->rest("post", [\kernel\modules\post\controllers\PostRestController::class]);
App::$collector->get('/', [\kernel\modules\post\controllers\PostRestController::class, 'actionIndex']);
App::$collector->get('/{id}', [\kernel\modules\post\controllers\PostRestController::class, 'actionView']);
// App::$collector->get('/page/{page_number}', [\kernel\modules\post\controllers\PostController::class, 'actionIndex']);
App::$collector->post('/create', [\kernel\modules\post\controllers\PostRestController::class, 'actionCreate']);
// App::$collector->post("/", [\kernel\modules\post\controllers\PostController::class, 'actionAdd']);
// App::$collector->get('/{id}', [\kernel\modules\post\controllers\PostController::class, 'actionView']);
// App::$collector->any('/update/{id}', [\kernel\modules\post\controllers\PostController::class, 'actionUpdate']);
// App::$collector->any("/edit/{id}", [\kernel\modules\post\controllers\PostController::class, 'actionEdit']);
App::$collector->get('/delete/{id}', [\kernel\modules\post\controllers\PostRestController::class, 'actionDelete']);
});
}); });