Skip to content

Commit d018b10

Browse files
committed
refactor(routes): simplified routes registration, improve code readability and simplified route callbacks
1 parent 23e40df commit d018b10

File tree

1 file changed

+124
-54
lines changed

1 file changed

+124
-54
lines changed

app/config/routes.php

Lines changed: 124 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -15,75 +15,147 @@
1515
*/
1616
// This processes github webhooks
1717
Flight::route(
18-
'POST /update-stuff',
19-
[DocsController::class, 'updateStuffPost'],
20-
false,
21-
'update_stuff'
18+
pattern: 'POST /update-stuff',
19+
callback: [DocsController::class, 'updateStuffPost'],
20+
alias: 'update_stuff'
2221
);
2322

2423
/*
2524
* Redirects
2625
*/
27-
// if theres no language or version in the url, redirect and default to en and v3
26+
// if theres no language or version in the url, redirect and default to en
27+
// and v3
2828
Flight::route('/', static function (): void {
2929
// pull out the default language by the accept header
3030
$language = Translator::getLanguageFromRequest();
31-
Flight::redirect('/' . $language . '/v3/');
31+
32+
Flight::redirect("/$language/v3/");
3233
});
3334

3435
// If the route only defines a language (ex: /en) redirect with a version
35-
Flight::route('/@language:[a-z0-9]{2}', static function (string $language): void {
36-
// if there's a number in it, it's actually probably the version so we'll need to pull the language out and consider this a version
37-
if (preg_match('/\d/', $language) === 1) {
38-
$version = $language;
39-
$language = Translator::getLanguageFromRequest();
40-
Flight::redirect("/en/$language/");
41-
} else {
42-
$version = 'v3';
36+
Flight::route(
37+
'/@language:[a-z0-9]{2}',
38+
static function (string $language): void {
39+
// if there's a number in it, it's actually probably the version so
40+
// we'll need to pull the language out and consider this a version
41+
$version = preg_match('/\d/', $language) ? $language : 'v3';
42+
43+
if (preg_match('/\d/', $language)) {
44+
$language = Translator::getLanguageFromRequest();
45+
Flight::redirect("/en/$language/");
46+
}
47+
48+
Flight::redirect("/$language/$version/");
4349
}
44-
Flight::redirect("/$language/$version/");
45-
});
50+
);
4651

4752
// Pick up old routes that didn't use to have a language and version header
48-
Flight::route('/@section:[\w\-]{3,}(/@sub_section:[\w\-]{3,})', function (string $section, ?string $sub_section = ''): void {
49-
$language = Translator::getLanguageFromRequest();
50-
Flight::redirect("/{$language}/v3/$section/$sub_section/");
51-
});
53+
Flight::route(
54+
'/@section:[\w\-]{3,}(/@sub_section:[\w\-]{3,})',
55+
static function (string $section, ?string $sub_section = ''): void {
56+
$language = Translator::getLanguageFromRequest();
57+
58+
Flight::redirect("/$language/v3/$section/$sub_section/");
59+
}
60+
);
5261

5362
/*
5463
* Core routes
5564
*/
56-
Flight::group('/@language:[a-z]{2}/@version:[a-z0-9]{2}', static function (): void {
57-
Flight::route('GET /', [DocsController::class, 'aboutGet'], false, 'about');
58-
Flight::route('GET /single-page', [DocsController::class, 'singlePageGet'], false, 'single_page');
59-
Flight::route('GET /about', [DocsController::class, 'aboutGet']);
60-
Flight::route('GET /install', [DocsController::class, 'installGet'], false, 'install');
61-
62-
// Unique URL workaround because this is the only 'single page' with a scrollspy for the time being.
63-
Flight::route('GET /install/install', static function (): void {
64-
Flight::redirect(Flight::getUrl('install'));
65-
});
66-
67-
Flight::route('GET /license', [DocsController::class, 'licenseGet'], false, 'license');
68-
Flight::route('GET /examples', [DocsController::class, 'examplesGet'], false, 'examples');
69-
Flight::route('GET /media', [DocsController::class, 'mediaGet'], false, 'media');
70-
Flight::route('GET /search', [DocsController::class, 'searchGet'], false, 'search');
71-
72-
Flight::group('/learn', static function (): void {
73-
Flight::route('GET ', [DocsController::class, 'learnGet'], false, 'learn');
74-
Flight::route('GET /@section_name', [DocsController::class, 'learnSectionsGet']);
75-
});
76-
77-
Flight::group('/guides', static function (): void {
78-
Flight::route('GET ', [DocsController::class, 'guidesGet'], false, 'guides');
79-
Flight::route('GET /@section_name', [DocsController::class, 'guidesSectionsGet']);
80-
});
81-
82-
Flight::group('/awesome-plugins', static function (): void {
83-
Flight::route('GET ', [DocsController::class, 'awesomePluginsGet'], false, 'awesome_plugins');
84-
Flight::route('GET /@plugin_name', [DocsController::class, 'pluginGet'], false, 'plugin');
85-
});
86-
});
65+
Flight::group(
66+
'/@language:[a-z]{2}/@version:[a-z0-9]{2}',
67+
static function (): void {
68+
Flight::route(
69+
pattern: 'GET /',
70+
callback: [DocsController::class, 'aboutGet'],
71+
alias: 'about'
72+
);
73+
74+
Flight::route(
75+
pattern: 'GET /single-page',
76+
callback: [DocsController::class, 'singlePageGet'],
77+
alias: 'single_page'
78+
);
79+
80+
Flight::route('GET /about', [DocsController::class, 'aboutGet']);
81+
82+
Flight::route(
83+
pattern: 'GET /install',
84+
callback: [DocsController::class, 'installGet'],
85+
alias: 'install'
86+
);
87+
88+
// Unique URL workaround because this is the only 'single page'
89+
// with a scrollspy for the time being.
90+
Flight::route('GET /install/install', static function (): void {
91+
Flight::redirect(Flight::getUrl('install'));
92+
});
93+
94+
Flight::route(
95+
pattern: 'GET /license',
96+
callback: [DocsController::class, 'licenseGet'],
97+
alias: 'license'
98+
);
99+
100+
Flight::route(
101+
pattern: 'GET /examples',
102+
callback: [DocsController::class, 'examplesGet'],
103+
alias: 'examples'
104+
);
105+
106+
Flight::route(
107+
pattern: 'GET /media',
108+
callback: [DocsController::class, 'mediaGet'],
109+
alias: 'media'
110+
);
111+
112+
Flight::route(
113+
pattern: 'GET /search',
114+
callback: [DocsController::class, 'searchGet'],
115+
alias: 'search'
116+
);
117+
118+
Flight::group('/learn', static function (): void {
119+
Flight::route(
120+
pattern: 'GET ',
121+
callback: [DocsController::class, 'learnGet'],
122+
alias: 'learn'
123+
);
124+
125+
Flight::route(
126+
'GET /@section_name',
127+
[DocsController::class, 'learnSectionsGet']
128+
);
129+
});
130+
131+
Flight::group('/guides', static function (): void {
132+
Flight::route(
133+
pattern: 'GET /',
134+
callback: [DocsController::class, 'guidesGet'],
135+
alias: 'guides'
136+
);
137+
138+
Flight::route(
139+
'GET /@section_name',
140+
[DocsController::class, 'guidesSectionsGet']
141+
);
142+
});
143+
144+
Flight::group('/awesome-plugins', static function (): void {
145+
Flight::route(
146+
pattern: 'GET /',
147+
callback: [DocsController::class, 'awesomePluginsGet'],
148+
alias: 'awesome_plugins'
149+
);
150+
151+
Flight::route(
152+
pattern: 'GET /@plugin_name',
153+
callback: [DocsController::class, 'pluginGet'],
154+
alias: 'plugin'
155+
);
156+
});
157+
}
158+
);
87159
}, [HeaderSecurityMiddleware::class]);
88160

89161
/*
@@ -95,14 +167,12 @@
95167

96168
// pull the version out of the URL
97169
$url = Flight::request()->url;
98-
$version = preg_match('~/(v\d)/~', $url, $matches) === 1 ? $matches[1] : 'v3';
170+
$version = preg_match('~/(v\d)/~', $url, $matches) ? $matches[1] : 'v3';
99171

100172
(new Container)->get(DocsLogic::class)->renderPage('not_found.latte', [
101173
'title' => '404 Not Found',
102174
'version' => $version
103175
]);
104176

105177
Flight::response()->send();
106-
107-
exit;
108178
});

0 commit comments

Comments
 (0)