mirror of
https://github.com/flarum/core.git
synced 2025-07-26 11:10:41 +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:
@@ -88,9 +88,13 @@ class RouteCollection
|
||||
|
||||
public function getPath($name, array $parameters = [])
|
||||
{
|
||||
$parts = $this->reverse[$name][0];
|
||||
array_walk($parts, [$this, 'fixPathPart'], $parameters);
|
||||
if (isset($this->reverse[$name])) {
|
||||
$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");
|
||||
}
|
||||
}
|
||||
|
@@ -13,10 +13,11 @@ namespace Flarum\Http;
|
||||
|
||||
use Flarum\Core;
|
||||
|
||||
class UrlGenerator implements UrlGeneratorInterface
|
||||
class UrlGenerator
|
||||
{
|
||||
protected $routes;
|
||||
|
||||
protected $prefix;
|
||||
|
||||
public function __construct(RouteCollection $routes)
|
||||
{
|
||||
@@ -28,11 +29,11 @@ class UrlGenerator implements UrlGeneratorInterface
|
||||
$path = $this->routes->getPath($name, $parameters);
|
||||
$path = ltrim($path, '/');
|
||||
|
||||
return Core::url() . "/$path";
|
||||
return Core::url($this->prefix) . "/$path";
|
||||
}
|
||||
|
||||
public function toAsset($path)
|
||||
{
|
||||
return Core::url() . "/assets/$path";
|
||||
return Core::url($this->prefix) . "/assets/$path";
|
||||
}
|
||||
}
|
||||
|
@@ -1,19 +0,0 @@
|
||||
<?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\Http;
|
||||
|
||||
interface UrlGeneratorInterface
|
||||
{
|
||||
public function toRoute($name, $parameters = []);
|
||||
|
||||
public function toAsset($path);
|
||||
}
|
Reference in New Issue
Block a user