mirror of
https://github.com/flarum/core.git
synced 2025-07-20 16:21:18 +02:00
Add API to add routes
This commit is contained in:
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
use Illuminate\Contracts\Container\Container;
|
use Illuminate\Contracts\Container\Container;
|
||||||
use Flarum\Forum\Actions\IndexAction;
|
use Flarum\Forum\Actions\IndexAction;
|
||||||
|
use Psr\Http\Message\ServerRequestInterface;
|
||||||
|
|
||||||
class ForumClient implements ExtenderInterface
|
class ForumClient implements ExtenderInterface
|
||||||
{
|
{
|
||||||
@@ -9,6 +10,8 @@ class ForumClient implements ExtenderInterface
|
|||||||
|
|
||||||
protected $translations = [];
|
protected $translations = [];
|
||||||
|
|
||||||
|
protected $routes = [];
|
||||||
|
|
||||||
public function assets($assets)
|
public function assets($assets)
|
||||||
{
|
{
|
||||||
$this->assets = array_merge($this->assets, $assets);
|
$this->assets = array_merge($this->assets, $assets);
|
||||||
@@ -23,12 +26,36 @@ class ForumClient implements ExtenderInterface
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function route($method, $url, $name, $action = 'Flarum\Forum\Actions\IndexAction')
|
||||||
|
{
|
||||||
|
$this->routes[] = compact('method', 'url', 'name', 'action');
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
public function extend(Container $container)
|
public function extend(Container $container)
|
||||||
{
|
{
|
||||||
|
if ($container->make('type') !== 'forum') {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
$container->make('events')->listen('Flarum\Forum\Events\RenderView', function ($event) {
|
$container->make('events')->listen('Flarum\Forum\Events\RenderView', function ($event) {
|
||||||
$event->assets->addFiles($this->assets);
|
$event->assets->addFiles($this->assets);
|
||||||
});
|
});
|
||||||
|
|
||||||
IndexAction::$translations = array_merge(IndexAction::$translations, $this->translations);
|
IndexAction::$translations = array_merge(IndexAction::$translations, $this->translations);
|
||||||
|
|
||||||
|
if (count($this->routes)) {
|
||||||
|
$routes = $container->make('flarum.forum.routes');
|
||||||
|
|
||||||
|
foreach ($this->routes as $route) {
|
||||||
|
$method = $route['method'];
|
||||||
|
$routes->$method($route['url'], $route['name'], function (ServerRequestInterface $httpRequest, $routeParams) use ($container, $route) {
|
||||||
|
$action = $container->make($route['action']);
|
||||||
|
|
||||||
|
return $action->handle($httpRequest, $routeParams);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user