Path Identity Resolver
Want to use a portion of the URLs path to identify the tenant? Well, the path identity resolver is here for that exact purpose
Introduction
The path identity resolver is a driver for Sprouts identify resolver functionality that uses a portion of the URLs path to identify a tenant.
Configuring
When configuring your resolver in the multitenancy config you only need
to do one thing to use this identity resolver, set the driver to path
.
An example config entry for this identity resolver looks like so.
1'resolvers' => [2 'path' => [3 'driver' => 'path',4 ],5]
As the path 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 'path' => [3 'driver' => 'path',4 'pattern' => '.*',5 'parameter' => '{tenancy}_resolved_by_{resolver}'6 ],7]
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}
.
As all route parameter-based identity resolvers can also be used without a route,
the path identity resolver will need to know which segment of the path contains should contain the tenants'
identity, which can be provided using the segment
config option.
1'resolvers' => [2 'path' => [3 'driver' => 'path',4 'segment' => 1,5 ],6]
This value is 1-index, so should not go lower than 1. It defaults to 1
.
Using
To make use of this identity resolver, once configured, either set the default resolver to its name.
1'defaults' => [2 //...3 'resolver' => 'path',4]
Or provide its name as the second argument when registering tenanted routes.
1Route::tenanted(function () {2 // Define tenant routes here3}, 'path');
The identity resolver itself is the Sprout\Http\Resolvers\PathIdentityResolver
class
and has a few additional methods that may be of use.
1public function getSegment(): int2 3public function getRoutePrefix(Tenancy $tenancy): string4 5public function getTenantRoutePrefix(Tenancy $tenancy): string
The getSegment()
method will return the config value segment
.
The getRoutePrefix()
method will return the path prefix used for the route definition (/{parameter}/
),
and the getTenantRoutePrefix()
will return the route prefix 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.path
to the current path,
also during the setup phase.