mirror of
https://github.com/flarum/framework.git
synced 2025-02-21 07:03:38 +08:00
Allow for the addition of new API endpoints
This commit is contained in:
parent
1d5586165c
commit
8a83d01bba
49
framework/core/src/Events/RegisterApiRoutes.php
Normal file
49
framework/core/src/Events/RegisterApiRoutes.php
Normal file
@ -0,0 +1,49 @@
|
||||
<?php namespace Flarum\Events;
|
||||
|
||||
use Flarum\Api\Request;
|
||||
use Flarum\Http\RouteCollection;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
|
||||
class RegisterApiRoutes
|
||||
{
|
||||
/**
|
||||
* @var RouteCollection
|
||||
*/
|
||||
public $routes;
|
||||
|
||||
/**
|
||||
* @param RouteCollection $routes
|
||||
*/
|
||||
public function __construct(RouteCollection $routes)
|
||||
{
|
||||
$this->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);
|
||||
};
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user