Subdomain Identity Resolver
Want to use the subdomain portion of the URL to identity a tenant? Well, the subdomain identity resolver is here for that exact purpose
Introduction
The subdomain identity resolver is a driver for Sprouts identify resolver functionality that uses the subdomain portion of the URL to identify a tenant.
Configuring
When configuring your resolver in the multitenancy config you only need
to do two things to use this identity resolver.
Set the driver to subdomain
and provide a parent domain using the domain
config option.
An example config entry for this identity resolver looks like so.
1'resolvers' => [2 'subdomain' => [3 'driver' => 'subdomain',4 'domain' => 'mydomain.com'5 ],6]
The default config uses a TENANTED_DOMAIN
environment variable,
and it is recommended that you use an environment variable, whether this one or another,
to provide the domain
config value.
As the subdomain identity resolver is a route parameter-based identity resolver,
it accepts two other optional config values.
The first is pattern
,
which allows you
to provide
a regular expression constraint for
the parameter,
and the second is parameter
that lets you provide the parameter name.
1'resolvers' => [2 'subdomain' => [3 'driver' => 'subdomain',4 'domain' => 'mydomain.com'5 'pattern' => '.*',6 'parameter' => '{tenancy}_resolved_by_{resolver}'7 ],8]
When providing a custom parameter name, you can use the placeholders {tenancy}
and {resolver}
.
{tenancy}
will be replaced by the registered name of the current tenancy,
and {resolver}
will be replaced by the registered name of the identity resolver.
The default value is {tenancy}_{resolver}
.
Using
To make use of this identity resolver, once configured, either set the default resolver to its name.
1'defaults' => [2 //...3 'resolver' => 'subdomain',4]
Or provide its name as the second argument when registering tenanted routes.
1Route::tenanted(function () {2 // Define tenant routes here3}, 'subdomain');
The identity resolver itself is the Sprout\Http\Resolvers\SubdomainIdentityResolver
class
and has a few additional methods that may be of use.
1public function getDomain(): string2 3public function getRouteDomain(Tenancy $tenancy): string4 5public function getTenantRouteDomain(Tenancy $tenancy): string
The getDomain()
method will return the config value domain
.
The getRouteDomain()
method will return domain used for the route definition ({parameter}.domain-setting
),
and the getTenantRouteDomain()
will return the fully qualified domain for the current tenant of the provided tenancy.
Side Effects
As a route parameter-based identity resolver, a default value for the route parameter will be set during the setup phase of the identity resolver (when a tenant becomes the current tenant). This means that if you need to generate a URL in the future from a route that has a parameter from this identity resolver, you don't need to provide it.
It also sets the internal Sprout setting url.domain
to the current domain,
also during the setup phase.