I have one project which has subdomaining set up with CORS middleware and other stuff to make it work. My Kernel.php (the important part) looks like this:
protected $middleware = [
\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
\App\Http\Middleware\TrimStrings::class,
\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
\App\Http\Middleware\TrustProxies::class,
];
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
// \Illuminate\Session\Middleware\AuthenticateSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
Cors::class,
EnforceAjax::class,
],
];
Now the thing is that I wanted to enforce AJAX on AJAX routes, so the EnforceAjax middleware looks like this:
public function handle($request, Closure $next)
{
$parsedUrl = parse_url($request->url());
$routeHasAjaxSuffix = key_exists('path', $parsedUrl) && strpos($parsedUrl['path'], '-ajax') !== false;
if ($routeHasAjaxSuffix && !$request->ajax()) {
return response('Bad request', 400);
}
return $next($request);
}
Now the problem I have is that when I am on my main domain, the requests which are sent to JS function properly, and are read as AJAX request.
However, the same request done from subdomain returns Bad request.
If I comment out the EnforceAjax then everything works well on subdomain also, so I believe that the CORS is set up correctly.
Somewhere in the process it looks to me that AJAX request stops being an AJAX request, though in my right mind that doesn't make any sense. Does anyone know what might be the problem here?
Subdomain routes are set up like this (they are working as expected with other non-ajax requests):
Route::domain('{region}.' . env('APP_URL'))->group(function () {
...
});