-
Notifications
You must be signed in to change notification settings - Fork 0
Routes by domain
macropay-solutions edited this page Aug 8, 2025
·
1 revision
One of the drawbacks of lumen’s and maravel's router (nikic/fast-route) is the lack of domain handling.
Lumen mentions domain key only in one place but it is never used:
// \Laravel\Lumen\Routing\Router::mergeGroup
/**
* Merge the given group attributes.
*
* @param array $new
* @param array $old
* @return array
*/
public function mergeGroup($new, $old)
{
$new['namespace'] = static::formatUsesPrefix($new, $old);
$new['prefix'] = static::formatGroupPrefix($new, $old);
if (isset($new['domain'])) {
unset($old['domain']);
}
if (isset($old['as'])) {
$new['as'] = $old['as'].(isset($new['as']) ? '.'.$new['as'] : '');
}
if (isset($old['suffix']) && ! isset($new['suffix'])) {
$new['suffix'] = $old['suffix'];
}
return array_merge_recursive(Arr::except($old, ['namespace', 'prefix', 'as', 'suffix']), $new);
}So, using it like this makes no difference:
$router->group(
[
'domain' => \env('APP_URL')
],
function (Router $router): void {
$router->post('/login', 'Auth\AuthController@login');
}
);Some might say Symfony 4.1 from Laravel is faster and handles also domains but, for those who still use lumen/maravel this is a way of restricting or registering the routes by domains:
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
class DomainMiddleware
{
public function handle(Request $request, Closure $next, string $urlEncodedCsvUrls): mixed
{
if (\str_contains(\urldecode($urlEncodedCsvUrls), $request->getHost())) {
return $next($request);
}
return \response(status: 404);
}
}// bootstrap/app.php
$app->routeMiddleware([
'url.encoded.domains' => \App\Http\Middleware\DomainMiddleware::class,
]);// routes/web.php
$router->group(
[
'middleware' => [
'url.encoded.domains:' . \urlencode(\rtrim(\env('APP_URL2'), ',') . ',' . \env('APP_URL'))
],
function (Router $router): void {
$router->post('/login', 'Auth\AuthController@login');
}
);