diff --git a/framework/core/src/Events/RegisterApiRoutes.php b/framework/core/src/Events/RegisterApiRoutes.php new file mode 100644 index 000000000..943a9437a --- /dev/null +++ b/framework/core/src/Events/RegisterApiRoutes.php @@ -0,0 +1,49 @@ +routes = $routes; + } + + public function get($url, $name, $action) + { + $this->route('get', $url, $name, $action); + } + + public function patch($url, $name, $action) + { + $this->route('patch', $url, $name, $action); + } + + protected function route($method, $url, $name, $action) + { + $this->routes->$method($url, $name, $this->action($action)); + } + + protected function action($class) + { + return function (ServerRequestInterface $httpRequest, $routeParams) use ($class) { + $action = app($class); + $actor = app('flarum.actor'); + + $input = array_merge($httpRequest->getQueryParams(), $httpRequest->getAttributes(), $routeParams); + $request = new Request($input, $actor, $httpRequest); + + return $action->handle($request); + }; + } +}