Centralized how routes are configured to support multi-segment slugs

This commit is contained in:
Alejandro Celaya
2022-08-06 09:30:13 +02:00
parent 4629f1b03f
commit 16bd368a58
10 changed files with 113 additions and 46 deletions

View File

@@ -7,23 +7,19 @@ namespace Shlinkio\Shlink;
use Fig\Http\Message\RequestMethodInterface;
use RKA\Middleware\IpAddress;
use Shlinkio\Shlink\Core\Action as CoreAction;
use Shlinkio\Shlink\Core\Config\EnvVars;
use Shlinkio\Shlink\Rest\Action;
use Shlinkio\Shlink\Rest\ConfigProvider;
use Shlinkio\Shlink\Rest\Middleware;
use Shlinkio\Shlink\Rest\Middleware\Mercure\NotConfiguredMercureErrorHandler;
use function sprintf;
// The order of the routes defined here matters. Changing it might cause path conflicts
return (static function (): array {
$contentNegotiationMiddleware = Middleware\ShortUrl\CreateShortUrlContentNegotiationMiddleware::class;
$dropDomainMiddleware = Middleware\ShortUrl\DropDefaultDomainFromRequestMiddleware::class;
$overrideDomainMiddleware = Middleware\ShortUrl\OverrideDomainMiddleware::class;
$multiSegment = (bool) EnvVars::MULTI_SEGMENT_SLUGS_ENABLED->loadFromEnv(false);
return [
// The order of the routes defined here matters. Changing it might cause path conflicts
'routes' => [
// Rest
...ConfigProvider::applyRoutesPrefix([
@@ -64,7 +60,7 @@ return (static function (): array {
Action\Domain\DomainRedirectsAction::getRouteDef(),
Action\MercureInfoAction::getRouteDef([NotConfiguredMercureErrorHandler::class]),
], $multiSegment),
]),
// Non-rest
[
@@ -77,7 +73,7 @@ return (static function (): array {
],
[
'name' => CoreAction\PixelAction::class,
'path' => sprintf('/{shortCode%s}/track', $multiSegment ? ':.+' : ''),
'path' => '/{shortCode}/track',
'middleware' => [
IpAddress::class,
CoreAction\PixelAction::class,
@@ -86,7 +82,7 @@ return (static function (): array {
],
[
'name' => CoreAction\QrCodeAction::class,
'path' => sprintf('/{shortCode%s}/qr-code', $multiSegment ? ':.+' : ''),
'path' => '/{shortCode}/qr-code',
'middleware' => [
CoreAction\QrCodeAction::class,
],
@@ -94,7 +90,7 @@ return (static function (): array {
],
[
'name' => CoreAction\RedirectAction::class,
'path' => sprintf('/{shortCode%s}', $multiSegment ? ':.+' : ''),
'path' => '/{shortCode}',
'middleware' => [
IpAddress::class,
CoreAction\RedirectAction::class,

View File

@@ -47,4 +47,5 @@ return (new ConfigAggregator\ConfigAggregator([
new ConfigAggregator\PhpFileProvider('config/autoload/routes.config.php'),
], 'data/cache/app_config.php', [
Core\Config\BasePathPrefixer::class,
Core\Config\MultiSegmentSlugProcessor::class,
]))->getMergedConfig();