Universal routes
Universal routes refer to routes that are usable by both the central app and the tenant app.
In other words: tenancy is initialized when a tenant is specified, and it’s not initialized when a tenant is not specified.
Usage
Since Tenancy v4, making a route universal is as easy as adding the universal
middleware on top of identification middleware:
If your central domain were your-saas.com
and a tenant’s domain were your-saas.theirdomain.com
:
- Tenancy would not be initialized on
your-saas.com/posts
- Tenancy would be initialized on
your-saas.theirdomain.com/posts
How tenants are specified
Above we said that tenancy is initialized if the tenant is specified. What exactly does this mean?
It refers to providing the tenant in some format that the used identification middleware expects.
With the domain example above it’s obvious, requests made on a domain that’s included in the central_domains
config will be central requests, and requests made on other domains will be tenant requests.
But how does this work with other identification middleware?
With request data identification, tenancy will be initialized if the tenant is provided in any of the supported formats:
With path identification, it’s a bit more complex.
Path identification
When you’re using path identification, you need to include the {tenant}
parameter in your routes.
However, the parameter is part of the route path, and the path is how Laravel distinguishes between different routes.
Therefore, we cannot have a route that works as both /posts
and /{tenant}/posts
.
Instead, We will need two different routes.
So since we’ll be using two different routes, we’ll need to register each route with path tenant identification that we’ll want to use universally in two ways:
/posts
(central route, no identification middleware)/{tenant}/posts
(tenant route, with identification middleware)
To make this easier for you, we have a dedicated feature for this: cloning routes as tenant routes.
Imagine that you’re integrating with a third-party package that has standard routes without any {tenant}
parameters:
In this example, we can change the middleware the package applies on its routes.
So if we apply the following middleware:
We’ll just need to create clones of these routes prefixed with /{tenant}/
to be able to use them in the tenant application.
For that, we’ll use the CloneRoutesAsTenant
action:
This action will create a copy of all routes that use path identification, have the universal
middleware flag, and don’t have the {tenant}
parameter, with the following changes:
- Prefix the route path with
/{tenant}/
- Prefix the route name with
tenant.
- Apply the
tenant
flag (to make tenancy initialization work if we’d be using early identification)
The example routes above would be cloned like this:
The original routes will be accessible in the central app (even if they have the identification middleware) because they have the universal
flag. They will not be accessible in the tenant app since there’s no {tenant}
parameter that could be specified.
The cloned routes will only be accessible in the tenant app (since they have the {tenant}
parameter) and tenancy will be initialized on them.
The route names are prefixed with tenant.
to avoid collisions while still letting you use named routes.
Default route mode universal
todo