1
0
mirror of https://github.com/flarum/core.git synced 2025-10-12 15:34:26 +02:00

Add multiple UrlGenerator classes for forum/api/admin

Spent quite a while looking into the best solution here and ended up going with three separate classes. Thanks to @Luceos for the PR that got this rolling (#518). My reasoning is:

- The task of routing and URL generation is independent for each section of the app. Take Flarum\Api\Users\IndexAction for example. I don't want to generate a URL to a Flarum route... I specifically want to generate a URL to an API route. So there should be a class with that specific responsibility.
- In fact, each URL generator is slightly different, because we need to add a certain prefix to the start (e.g. /api)
- This also allows us to get rid of the "flarum.api" prefix on each route's name.
- It's still DRY, because they all extend a base class.

At the same time, I could see no reason this needed to be "interfaced", so all of the classes are concrete.

Goes a long way to fixing #123 - still just a few places left remaining with hardcoded URLs.
This commit is contained in:
Toby Zerner
2015-10-02 17:35:29 +09:30
parent 9e91ada4a8
commit f255d318ef
15 changed files with 124 additions and 85 deletions

View File

@@ -13,7 +13,7 @@ namespace Flarum\Forum;
use Flarum\Core\Users\Guest;
use Flarum\Events\RegisterForumRoutes;
use Flarum\Http\RouteCollection;
use Flarum\Http\UrlGenerator;
use Flarum\Forum\UrlGenerator;
use Flarum\Support\ServiceProvider;
use Psr\Http\Message\ServerRequestInterface;
@@ -31,7 +31,7 @@ class ForumServiceProvider extends ServiceProvider
});
$this->app->singleton(
'Flarum\Http\UrlGeneratorInterface',
UrlGenerator::class,
function () {
return new UrlGenerator($this->app->make('flarum.forum.routes'));
}
@@ -62,67 +62,67 @@ class ForumServiceProvider extends ServiceProvider
$routes->get(
'/all',
'flarum.forum.index',
'index',
$defaultAction = $this->action('Flarum\Forum\Actions\IndexAction')
);
$routes->get(
'/d/{id:\d+(?:-[^/]*)?}[/{near:[^/]*}]',
'flarum.forum.discussion',
'discussion',
$this->action('Flarum\Forum\Actions\DiscussionAction')
);
$routes->get(
'/u/{username}[/{filter:[^/]*}]',
'flarum.forum.user',
'user',
$this->action('Flarum\Forum\Actions\ClientAction')
);
$routes->get(
'/settings',
'flarum.forum.settings',
'settings',
$this->action('Flarum\Forum\Actions\ClientAction')
);
$routes->get(
'/notifications',
'flarum.forum.notifications',
'notifications',
$this->action('Flarum\Forum\Actions\ClientAction')
);
$routes->get(
'/logout',
'flarum.forum.logout',
'logout',
$this->action('Flarum\Forum\Actions\LogoutAction')
);
$routes->post(
'/login',
'flarum.forum.login',
'login',
$this->action('Flarum\Forum\Actions\LoginAction')
);
$routes->post(
'/register',
'flarum.forum.register',
'register',
$this->action('Flarum\Forum\Actions\RegisterAction')
);
$routes->get(
'/confirm/{token}',
'flarum.forum.confirmEmail',
'confirmEmail',
$this->action('Flarum\Forum\Actions\ConfirmEmailAction')
);
$routes->get(
'/reset/{token}',
'flarum.forum.resetPassword',
'resetPassword',
$this->action('Flarum\Forum\Actions\ResetPasswordAction')
);
$routes->post(
'/reset',
'flarum.forum.savePassword',
'savePassword',
$this->action('Flarum\Forum\Actions\SavePasswordAction')
);
@@ -137,7 +137,7 @@ class ForumServiceProvider extends ServiceProvider
$routes->get(
'/',
'flarum.forum.default',
'default',
$defaultAction
);
}