I serve multiple subdomains with Laravel with this code here:
RouteServiceProvider.php
protected function mapSubdomainRoutes()
{
Route::group([
'namespace' => $this->namespace,
'domain' => '{subdomain}.affekt.de',
], function () {
require base_path('routes/web.php');
});
}
But watch out you have an new argument then for your controllers. It's $subdomain. I remove this argument with a custom middleware. Because I don't want to add $subdomain argument to all my controllers.
$route->forgetParameter('subdomain'); will remove it.
<?php
namespace App\Http\Middleware;
use Closure;
class RemoveSubdomainArgs
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$route = $request->route();
$route->forgetParameter('subdomain');
return $next($request);
}
}
Don't forget to add the new RemoveSubdomainArgs middleware to your HTTP kernel.
are you supposed to put this code in the main domain or subdomain's providers and middleware? I followed your instructions on this page and here https://laracasts.com/discuss/channels/servers/subdomain-map-to-subdirectory-route-in-existing-laravel-project
But they didn't work.