Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Monaam's avatar

Skip a routes group when middleware validation fails

Hello hellow !

So i am building a multitenant app, users and accounts are stored in the main database. The app has multiple tenant types, each type uses a different database connection, and the app knows which connection to use with $account->connection Each tenant type has different routes structures, so I've put each tenant routes in a different file under the routes folder

  • routes/main/web.php
  • routes/type1/web.php
  • routes/type2/web.php

And mapped them all in the routes service provider. And i am using a middleware for each tenant type.

  • Type1Init
  • Type2Init

For my routes, i use the domain parameter as an point of entry Route::pattern('account_host', '([\da-z\.-]+)\.([a-z]{2,6})+');

And for each folder i use :

Route::middleware(['web', 'type1init'])
             ->namespace($this->namespace.'\Type1')
             ->name('type1.')
             ->domain('{account_host}')
             ->group(base_path('routes/type1/web.php'));
Route::middleware(['web', 'type2init'])
             ->namespace($this->namespace.'\Type2')
             ->name('type2.')
             ->domain('{account_host}')
             ->group(base_path('routes/type2/web.php'));

And in each middleware i verify if the account has a valid subscription, The problem is, when i access an account in the second tenant type, i get errors cause the app only access the first group of routes

My question is, can we use middlewares to skip the first group and goes to the second one? third one?

PS: I know i can use dynamic routes loading, but i actually want to cache all the routes for faster execution, so i am asking for that perticular question :)

Thanks !

0 likes
6 replies
martinbean's avatar

@Monaam I really don’t understand your question. You should have a middleware class that checks the subscription, and then return an error there. That’s what I do in my multi-tenant application:

class CheckForActiveSubscription
{
    public function handle($request, Closure $next)
    {
        if ($request->account->subscribed('default')) {
            return $next($request);
        }

        abort(503);
    }
}

This way, it doesn’t matter what group is handling the request, as you can use the middleware class anywhere.

A good rule of thumb for structuring your applications’ code: a class should do one thing, and one thing well.

Monaam's avatar

@martinbean the thing is, he might not have an active subscription for the first group of websites, but he has one in the second group I want to skip the whole routes group to the next one

martinbean's avatar

@monaam Surely a single website should only belong to one single route group? Otherwise I don’t think you’ve understand route groups properly.

Monaam's avatar

@martinbean this is a multi-tenant app, it's not a single website, and we provide multiple types of websites, some of the clients belongs to the first type, others to the second, and we want it scalable so we can reach 30 different types. Each type has his routes, but the problem is how Laravel routes works, we catch all clients using their domain name, we cannot add other conditions to the route group

martinbean's avatar

@Monaam But a user can only view one site at a time? So have a middleware class that checks if the user is subscribed to the site that they’re trying to access as part of that particular HTTP request.

Please or to participate in this conversation.