1
0
mirror of https://github.com/flarum/core.git synced 2025-07-20 00:01:17 +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 7a927fbe18
commit 127c54bc1c
15 changed files with 124 additions and 85 deletions

View File

@@ -11,7 +11,7 @@
namespace Flarum\Admin; namespace Flarum\Admin;
use Flarum\Http\RouteCollection; use Flarum\Http\RouteCollection;
use Flarum\Http\UrlGenerator; use Flarum\Admin\UrlGenerator;
use Illuminate\Support\ServiceProvider; use Illuminate\Support\ServiceProvider;
use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ServerRequestInterface;
use Zend\Diactoros\Response\RedirectResponse; use Zend\Diactoros\Response\RedirectResponse;
@@ -26,7 +26,7 @@ class AdminServiceProvider extends ServiceProvider
public function register() public function register()
{ {
$this->app->singleton( $this->app->singleton(
'Flarum\Http\UrlGeneratorInterface', UrlGenerator::class,
function () { function () {
return new UrlGenerator($this->app->make('flarum.admin.routes')); return new UrlGenerator($this->app->make('flarum.admin.routes'));
} }
@@ -57,7 +57,7 @@ class AdminServiceProvider extends ServiceProvider
$routes->get( $routes->get(
'/', '/',
'flarum.admin.index', 'index',
$this->action('Flarum\Admin\Actions\ClientAction') $this->action('Flarum\Admin\Actions\ClientAction')
); );
} }

View File

@@ -0,0 +1,18 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Admin;
use Flarum\Http\UrlGenerator as BaseUrlGenerator;
class UrlGenerator extends BaseUrlGenerator
{
protected $prefix = 'admin';
}

View File

@@ -14,7 +14,7 @@ use Flarum\Core\Search\SearchCriteria;
use Flarum\Core\Discussions\Search\DiscussionSearcher; use Flarum\Core\Discussions\Search\DiscussionSearcher;
use Flarum\Api\Actions\SerializeCollectionAction; use Flarum\Api\Actions\SerializeCollectionAction;
use Flarum\Api\JsonApiRequest; use Flarum\Api\JsonApiRequest;
use Flarum\Http\UrlGeneratorInterface; use Flarum\Api\UrlGenerator;
use Tobscure\JsonApi\Document; use Tobscure\JsonApi\Document;
class IndexAction extends SerializeCollectionAction class IndexAction extends SerializeCollectionAction
@@ -25,7 +25,7 @@ class IndexAction extends SerializeCollectionAction
protected $searcher; protected $searcher;
/** /**
* @var UrlGeneratorInterface * @var UrlGenerator
*/ */
protected $url; protected $url;
@@ -74,9 +74,9 @@ class IndexAction extends SerializeCollectionAction
/** /**
* @param DiscussionSearcher $searcher * @param DiscussionSearcher $searcher
* @param UrlGeneratorInterface $url * @param UrlGenerator $url
*/ */
public function __construct(DiscussionSearcher $searcher, UrlGeneratorInterface $url) public function __construct(DiscussionSearcher $searcher, UrlGenerator $url)
{ {
$this->searcher = $searcher; $this->searcher = $searcher;
$this->url = $url; $this->url = $url;
@@ -106,7 +106,7 @@ class IndexAction extends SerializeCollectionAction
$this->addPaginationLinks( $this->addPaginationLinks(
$document, $document,
$request, $request,
$request->http ? $this->url->toRoute('flarum.api.discussions.index') : '', $request->http ? $this->url->toRoute('discussions.index') : '',
$results->areMoreResults() $results->areMoreResults()
); );

View File

@@ -14,7 +14,7 @@ use Flarum\Core\Search\SearchCriteria;
use Flarum\Core\Users\Search\UserSearcher; use Flarum\Core\Users\Search\UserSearcher;
use Flarum\Api\Actions\SerializeCollectionAction; use Flarum\Api\Actions\SerializeCollectionAction;
use Flarum\Api\JsonApiRequest; use Flarum\Api\JsonApiRequest;
use Flarum\Http\UrlGeneratorInterface; use Flarum\Api\UrlGenerator;
use Tobscure\JsonApi\Document; use Tobscure\JsonApi\Document;
class IndexAction extends SerializeCollectionAction class IndexAction extends SerializeCollectionAction
@@ -25,7 +25,7 @@ class IndexAction extends SerializeCollectionAction
protected $searcher; protected $searcher;
/** /**
* @var UrlGeneratorInterface * @var UrlGenerator
*/ */
protected $url; protected $url;
@@ -68,9 +68,9 @@ class IndexAction extends SerializeCollectionAction
/** /**
* @param UserSearcher $searcher * @param UserSearcher $searcher
* @param UrlGeneratorInterface $url * @param UrlGenerator $url
*/ */
public function __construct(UserSearcher $searcher, UrlGeneratorInterface $url) public function __construct(UserSearcher $searcher, UrlGenerator $url)
{ {
$this->searcher = $searcher; $this->searcher = $searcher;
$this->url = $url; $this->url = $url;
@@ -97,7 +97,7 @@ class IndexAction extends SerializeCollectionAction
$this->addPaginationLinks( $this->addPaginationLinks(
$document, $document,
$request, $request,
$this->url->toRoute('flarum.api.users.index'), $this->url->toRoute('users.index'),
$results->areMoreResults() $results->areMoreResults()
); );

View File

@@ -17,7 +17,7 @@ use Flarum\Events\RegisterApiRoutes;
use Flarum\Events\RegisterActivityTypes; use Flarum\Events\RegisterActivityTypes;
use Flarum\Events\RegisterNotificationTypes; use Flarum\Events\RegisterNotificationTypes;
use Flarum\Http\RouteCollection; use Flarum\Http\RouteCollection;
use Flarum\Http\UrlGenerator; use Flarum\Api\UrlGenerator;
use Illuminate\Support\ServiceProvider; use Illuminate\Support\ServiceProvider;
use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ServerRequestInterface;
@@ -35,7 +35,7 @@ class ApiServiceProvider extends ServiceProvider
}); });
$this->app->singleton( $this->app->singleton(
'Flarum\Http\UrlGeneratorInterface', UrlGenerator::class,
function () { function () {
return new UrlGenerator($this->app->make('flarum.api.routes')); return new UrlGenerator($this->app->make('flarum.api.routes'));
} }
@@ -78,28 +78,28 @@ class ApiServiceProvider extends ServiceProvider
// Get forum information // Get forum information
$routes->get( $routes->get(
'/forum', '/forum',
'flarum.api.forum.show', 'forum.show',
$this->action('Flarum\Api\Actions\Forum\ShowAction') $this->action('Flarum\Api\Actions\Forum\ShowAction')
); );
// Save forum information // Save forum information
$routes->patch( $routes->patch(
'/forum', '/forum',
'flarum.api.forum.update', 'forum.update',
$this->action('Flarum\Api\Actions\Forum\UpdateAction') $this->action('Flarum\Api\Actions\Forum\UpdateAction')
); );
// Retrieve authentication token // Retrieve authentication token
$routes->post( $routes->post(
'/token', '/token',
'flarum.api.token', 'token',
$this->action('Flarum\Api\Actions\TokenAction') $this->action('Flarum\Api\Actions\TokenAction')
); );
// Send forgot password email // Send forgot password email
$routes->post( $routes->post(
'/forgot', '/forgot',
'flarum.api.forgot', 'forgot',
$this->action('Flarum\Api\Actions\ForgotAction') $this->action('Flarum\Api\Actions\ForgotAction')
); );
@@ -112,49 +112,49 @@ class ApiServiceProvider extends ServiceProvider
// List users // List users
$routes->get( $routes->get(
'/users', '/users',
'flarum.api.users.index', 'users.index',
$this->action('Flarum\Api\Actions\Users\IndexAction') $this->action('Flarum\Api\Actions\Users\IndexAction')
); );
// Register a user // Register a user
$routes->post( $routes->post(
'/users', '/users',
'flarum.api.users.create', 'users.create',
$this->action('Flarum\Api\Actions\Users\CreateAction') $this->action('Flarum\Api\Actions\Users\CreateAction')
); );
// Get a single user // Get a single user
$routes->get( $routes->get(
'/users/{id}', '/users/{id}',
'flarum.api.users.show', 'users.show',
$this->action('Flarum\Api\Actions\Users\ShowAction') $this->action('Flarum\Api\Actions\Users\ShowAction')
); );
// Edit a user // Edit a user
$routes->patch( $routes->patch(
'/users/{id}', '/users/{id}',
'flarum.api.users.update', 'users.update',
$this->action('Flarum\Api\Actions\Users\UpdateAction') $this->action('Flarum\Api\Actions\Users\UpdateAction')
); );
// Delete a user // Delete a user
$routes->delete( $routes->delete(
'/users/{id}', '/users/{id}',
'flarum.api.users.delete', 'users.delete',
$this->action('Flarum\Api\Actions\Users\DeleteAction') $this->action('Flarum\Api\Actions\Users\DeleteAction')
); );
// Upload avatar // Upload avatar
$routes->post( $routes->post(
'/users/{id}/avatar', '/users/{id}/avatar',
'flarum.api.users.avatar.upload', 'users.avatar.upload',
$this->action('Flarum\Api\Actions\Users\UploadAvatarAction') $this->action('Flarum\Api\Actions\Users\UploadAvatarAction')
); );
// Remove avatar // Remove avatar
$routes->delete( $routes->delete(
'/users/{id}/avatar', '/users/{id}/avatar',
'flarum.api.users.avatar.delete', 'users.avatar.delete',
$this->action('Flarum\Api\Actions\Users\DeleteAvatarAction') $this->action('Flarum\Api\Actions\Users\DeleteAvatarAction')
); );
@@ -167,28 +167,28 @@ class ApiServiceProvider extends ServiceProvider
// List activity // List activity
$routes->get( $routes->get(
'/activity', '/activity',
'flarum.api.activity.index', 'activity.index',
$this->action('Flarum\Api\Actions\Activity\IndexAction') $this->action('Flarum\Api\Actions\Activity\IndexAction')
); );
// List notifications for the current user // List notifications for the current user
$routes->get( $routes->get(
'/notifications', '/notifications',
'flarum.api.notifications.index', 'notifications.index',
$this->action('Flarum\Api\Actions\Notifications\IndexAction') $this->action('Flarum\Api\Actions\Notifications\IndexAction')
); );
// Mark all notifications as read // Mark all notifications as read
$routes->post( $routes->post(
'/notifications/read', '/notifications/read',
'flarum.api.notifications.readAll', 'notifications.readAll',
$this->action('Flarum\Api\Actions\Notifications\ReadAllAction') $this->action('Flarum\Api\Actions\Notifications\ReadAllAction')
); );
// Mark a single notification as read // Mark a single notification as read
$routes->patch( $routes->patch(
'/notifications/{id}', '/notifications/{id}',
'flarum.api.notifications.update', 'notifications.update',
$this->action('Flarum\Api\Actions\Notifications\UpdateAction') $this->action('Flarum\Api\Actions\Notifications\UpdateAction')
); );
@@ -201,35 +201,35 @@ class ApiServiceProvider extends ServiceProvider
// List discussions // List discussions
$routes->get( $routes->get(
'/discussions', '/discussions',
'flarum.api.discussions.index', 'discussions.index',
$this->action('Flarum\Api\Actions\Discussions\IndexAction') $this->action('Flarum\Api\Actions\Discussions\IndexAction')
); );
// Create a discussion // Create a discussion
$routes->post( $routes->post(
'/discussions', '/discussions',
'flarum.api.discussions.create', 'discussions.create',
$this->action('Flarum\Api\Actions\Discussions\CreateAction') $this->action('Flarum\Api\Actions\Discussions\CreateAction')
); );
// Show a single discussion // Show a single discussion
$routes->get( $routes->get(
'/discussions/{id}', '/discussions/{id}',
'flarum.api.discussions.show', 'discussions.show',
$this->action('Flarum\Api\Actions\Discussions\ShowAction') $this->action('Flarum\Api\Actions\Discussions\ShowAction')
); );
// Edit a discussion // Edit a discussion
$routes->patch( $routes->patch(
'/discussions/{id}', '/discussions/{id}',
'flarum.api.discussions.update', 'discussions.update',
$this->action('Flarum\Api\Actions\Discussions\UpdateAction') $this->action('Flarum\Api\Actions\Discussions\UpdateAction')
); );
// Delete a discussion // Delete a discussion
$routes->delete( $routes->delete(
'/discussions/{id}', '/discussions/{id}',
'flarum.api.discussions.delete', 'discussions.delete',
$this->action('Flarum\Api\Actions\Discussions\DeleteAction') $this->action('Flarum\Api\Actions\Discussions\DeleteAction')
); );
@@ -242,35 +242,35 @@ class ApiServiceProvider extends ServiceProvider
// List posts, usually for a discussion // List posts, usually for a discussion
$routes->get( $routes->get(
'/posts', '/posts',
'flarum.api.posts.index', 'posts.index',
$this->action('Flarum\Api\Actions\Posts\IndexAction') $this->action('Flarum\Api\Actions\Posts\IndexAction')
); );
// Create a post // Create a post
$routes->post( $routes->post(
'/posts', '/posts',
'flarum.api.posts.create', 'posts.create',
$this->action('Flarum\Api\Actions\Posts\CreateAction') $this->action('Flarum\Api\Actions\Posts\CreateAction')
); );
// Show a single or multiple posts by ID // Show a single or multiple posts by ID
$routes->get( $routes->get(
'/posts/{id}', '/posts/{id}',
'flarum.api.posts.show', 'posts.show',
$this->action('Flarum\Api\Actions\Posts\ShowAction') $this->action('Flarum\Api\Actions\Posts\ShowAction')
); );
// Edit a post // Edit a post
$routes->patch( $routes->patch(
'/posts/{id}', '/posts/{id}',
'flarum.api.posts.update', 'posts.update',
$this->action('Flarum\Api\Actions\Posts\UpdateAction') $this->action('Flarum\Api\Actions\Posts\UpdateAction')
); );
// Delete a post // Delete a post
$routes->delete( $routes->delete(
'/posts/{id}', '/posts/{id}',
'flarum.api.posts.delete', 'posts.delete',
$this->action('Flarum\Api\Actions\Posts\DeleteAction') $this->action('Flarum\Api\Actions\Posts\DeleteAction')
); );
@@ -283,28 +283,28 @@ class ApiServiceProvider extends ServiceProvider
// List groups // List groups
$routes->get( $routes->get(
'/groups', '/groups',
'flarum.api.groups.index', 'groups.index',
$this->action('Flarum\Api\Actions\Groups\IndexAction') $this->action('Flarum\Api\Actions\Groups\IndexAction')
); );
// Create a group // Create a group
$routes->post( $routes->post(
'/groups', '/groups',
'flarum.api.groups.create', 'groups.create',
$this->action('Flarum\Api\Actions\Groups\CreateAction') $this->action('Flarum\Api\Actions\Groups\CreateAction')
); );
// Edit a group // Edit a group
$routes->patch( $routes->patch(
'/groups/{id}', '/groups/{id}',
'flarum.api.groups.update', 'groups.update',
$this->action('Flarum\Api\Actions\Groups\UpdateAction') $this->action('Flarum\Api\Actions\Groups\UpdateAction')
); );
// Delete a group // Delete a group
$routes->delete( $routes->delete(
'/groups/{id}', '/groups/{id}',
'flarum.api.groups.delete', 'groups.delete',
$this->action('Flarum\Api\Actions\Groups\DeleteAction') $this->action('Flarum\Api\Actions\Groups\DeleteAction')
); );
@@ -317,28 +317,28 @@ class ApiServiceProvider extends ServiceProvider
// Toggle an extension // Toggle an extension
$routes->patch( $routes->patch(
'/extensions/{name}', '/extensions/{name}',
'flarum.api.extensions.update', 'extensions.update',
$this->action('Flarum\Api\Actions\Extensions\UpdateAction') $this->action('Flarum\Api\Actions\Extensions\UpdateAction')
); );
// Uninstall an extension // Uninstall an extension
$routes->delete( $routes->delete(
'/extensions/{name}', '/extensions/{name}',
'flarum.api.extensions.delete', 'extensions.delete',
$this->action('Flarum\Api\Actions\Extensions\DeleteAction') $this->action('Flarum\Api\Actions\Extensions\DeleteAction')
); );
// Update config settings // Update config settings
$routes->post( $routes->post(
'/config', '/config',
'flarum.api.config', 'config',
$this->action('Flarum\Api\Actions\ConfigAction') $this->action('Flarum\Api\Actions\ConfigAction')
); );
// Update a permission // Update a permission
$routes->post( $routes->post(
'/permission', '/permission',
'flarum.api.permission', 'permission',
$this->action('Flarum\Api\Actions\PermissionAction') $this->action('Flarum\Api\Actions\PermissionAction')
); );

View File

@@ -0,0 +1,18 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Api;
use Flarum\Http\UrlGenerator as BaseUrlGenerator;
class UrlGenerator extends BaseUrlGenerator
{
protected $prefix = 'api';
}

View File

@@ -17,7 +17,7 @@ use Illuminate\Contracts\Mail\Mailer;
use Illuminate\Mail\Message; use Illuminate\Mail\Message;
use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Database\Eloquent\ModelNotFoundException;
use Flarum\Core; use Flarum\Core;
use Flarum\Http\UrlGeneratorInterface; use Flarum\Forum\UrlGenerator;
class RequestPasswordResetHandler class RequestPasswordResetHandler
{ {
@@ -37,7 +37,7 @@ class RequestPasswordResetHandler
protected $mailer; protected $mailer;
/** /**
* @var UrlGeneratorInterface * @var UrlGenerator
*/ */
protected $url; protected $url;
@@ -45,9 +45,9 @@ class RequestPasswordResetHandler
* @param UserRepository $users * @param UserRepository $users
* @param SettingsRepository $settings * @param SettingsRepository $settings
* @param Mailer $mailer * @param Mailer $mailer
* @param UrlGeneratorInterface $url * @param UrlGenerator $url
*/ */
public function __construct(UserRepository $users, SettingsRepository $settings, Mailer $mailer, UrlGeneratorInterface $url) public function __construct(UserRepository $users, SettingsRepository $settings, Mailer $mailer, UrlGenerator $url)
{ {
$this->users = $users; $this->users = $users;
$this->settings = $settings; $this->settings = $settings;

View File

@@ -328,7 +328,7 @@ class User extends Model
*/ */
public function getAvatarUrlAttribute() public function getAvatarUrlAttribute()
{ {
$urlGenerator = app('Flarum\Http\UrlGeneratorInterface'); $urlGenerator = app('Flarum\Forum\UrlGenerator');
return $this->avatar_path ? $urlGenerator->toAsset('avatars/'.$this->avatar_path) : null; return $this->avatar_path ? $urlGenerator->toAsset('avatars/'.$this->avatar_path) : null;
} }

View File

@@ -50,8 +50,8 @@ class DiscussionAction extends ClientAction
$queryString[] = $k . '=' . $v; $queryString[] = $k . '=' . $v;
} }
return app('Flarum\Http\UrlGeneratorInterface') return app('Flarum\Forum\UrlGenerator')
->toRoute('flarum.forum.discussion', ['id' => $document->data->id]) . ->toRoute('discussion', ['id' => $document->data->id]) .
($queryString ? '?' . implode('&', $queryString) : ''); ($queryString ? '?' . implode('&', $queryString) : '');
}; };

View File

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

View File

@@ -1,5 +1,4 @@
<?php <?php
/* /*
* This file is part of Flarum. * This file is part of Flarum.
* *
@@ -9,11 +8,10 @@
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
namespace Flarum\Http; namespace Flarum\Forum;
interface UrlGeneratorInterface use Flarum\Http\UrlGenerator as BaseUrlGenerator;
class UrlGenerator extends BaseUrlGenerator
{ {
public function toRoute($name, $parameters = []);
public function toAsset($path);
} }

View File

@@ -88,9 +88,13 @@ class RouteCollection
public function getPath($name, array $parameters = []) public function getPath($name, array $parameters = [])
{ {
$parts = $this->reverse[$name][0]; if (isset($this->reverse[$name])) {
array_walk($parts, [$this, 'fixPathPart'], $parameters); $parts = $this->reverse[$name][0];
array_walk($parts, [$this, 'fixPathPart'], $parameters);
return '/' . ltrim(implode('', $parts), '/'); return '/' . ltrim(implode('', $parts), '/');
}
throw new \RuntimeException("Route $name not found");
} }
} }

View File

@@ -13,10 +13,11 @@ namespace Flarum\Http;
use Flarum\Core; use Flarum\Core;
class UrlGenerator implements UrlGeneratorInterface class UrlGenerator
{ {
protected $routes; protected $routes;
protected $prefix;
public function __construct(RouteCollection $routes) public function __construct(RouteCollection $routes)
{ {
@@ -28,11 +29,11 @@ class UrlGenerator implements UrlGeneratorInterface
$path = $this->routes->getPath($name, $parameters); $path = $this->routes->getPath($name, $parameters);
$path = ltrim($path, '/'); $path = ltrim($path, '/');
return Core::url() . "/$path"; return Core::url($this->prefix) . "/$path";
} }
public function toAsset($path) public function toAsset($path)
{ {
return Core::url() . "/assets/$path"; return Core::url($this->prefix) . "/assets/$path";
} }
} }

View File

@@ -1,5 +1,5 @@
<?php <?php
$url = app('Flarum\Http\UrlGeneratorInterface'); $url = app('Flarum\Forum\UrlGenerator');
?> ?>
<div class="container"> <div class="container">
<h2>All Discussions</h2> <h2>All Discussions</h2>
@@ -7,7 +7,7 @@ $url = app('Flarum\Http\UrlGeneratorInterface');
<ul> <ul>
@foreach ($document->data as $discussion) @foreach ($document->data as $discussion)
<li> <li>
<a href="{{ $url->toRoute('flarum.forum.discussion', [ <a href="{{ $url->toRoute('discussion', [
'id' => $discussion->id . '-' . $discussion->attributes->title 'id' => $discussion->id . '-' . $discussion->attributes->title
]) }}"> ]) }}">
{{ $discussion->attributes->title }} {{ $discussion->attributes->title }}
@@ -16,5 +16,5 @@ $url = app('Flarum\Http\UrlGeneratorInterface');
@endforeach @endforeach
</ul> </ul>
<a href="{{ $url->toRoute('flarum.forum.index') }}?page={{ $page + 1 }}">Next Page &raquo;</a> <a href="{{ $url->toRoute('index') }}?page={{ $page + 1 }}">Next Page &raquo;</a>
</div> </div>

View File

@@ -11,7 +11,7 @@
<body> <body>
<h1>Reset Your Password</h1> <h1>Reset Your Password</h1>
<form class="form-horizontal" role="form" method="POST" action="{{ app('Flarum\Http\UrlGeneratorInterface')->toRoute('flarum.forum.savePassword') }}"> <form class="form-horizontal" role="form" method="POST" action="{{ app('Flarum\Forum\UrlGenerator')->toRoute('savePassword') }}">
<input type="hidden" name="token" value="{{ $token }}"> <input type="hidden" name="token" value="{{ $token }}">
<div class="form-group"> <div class="form-group">