I want to check if a subdomain exists before redirecting a user to the login form. So I want to apply the "website" middleware before the "auth" middleware.
web.php:
Route::domain('{website}.localhost')->middleware(['website', 'auth'])->group(function () {
Route::get('/', [HomeController::class, 'index'])->name('home');
//others routes
});
website middleware:
if(Website::where('domain_name', $request->route('website'))->exists())
{
return $next($request);
}
else
{
abort(404);
}
If I try to go to http://fakedomain.localhost I am redirected to the login form rather than getting a 404.
edit 1: I registered my middleware in the kernel.php
CodePudding user response:
You could try to adjust the middleware priorities so it will run this middleware before 'auth'. You can statically define the middleware priority list (which is already defined on the base Illuminate\Http\Kernel
) or you can dynamically prepend to the middleware priority list in a Service Provider.
In a Service Provider's boot
method:
use Illuminate\Http\Contracts\Kernel;
public function boot()
{
$this->app[Kernel::class]->prependToMiddlewarePriority(YourMiddleware::class);
}
Or you can statically define the list in your App\Http\Kernel
:
protected $middlewarePriority = [
\App\Http\Middleware\YourMiddleware::class,
\Illuminate\Cookie\Middleware\EncryptCookies::class,
...
];
Laravel 9.x Docs - Middleware - Registering Middleware -Sorting Middleware
CodePudding user response:
open Kernel.php
add in $routeMiddleware
array your Middleware
protected $routeMiddleware = [
'website' => YourMiddlewareClass::class
];