Session Identity Resolver
Sometimes you'll want to identify a tenant without having the identifier in the URL, in a situation where you can't expect a HTTP header to be provided, and when you want something a little more dynamic than a cookie. For those situations, there's the session identity resolver
Introduction
The session identity resolver is a driver for Sprouts identity resolver functionality that uses a value stored in the session to identify a tenant.
This identity resolver does not use the URL, so you cannot generate a link for a specific tenant.
This identity resolver is not compatible with the session service override.
This identity resolver can only be used during the middleware hook, as sessions are not initialised until the middleware responsible for it has been run.
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 session
.
An example config entry for this identity resolver looks like so.
1'resolvers' => [2 'session' => [3 'driver' => 'session',4 ],5]
You can also optionally provide the name of the session using the session
config option.
This option allows you to provide placeholders like the other identity resolvers and their route parameters,
and will default to multitenancy.{tenancy}
.
1'resolvers' => [2 'session' => [3 'driver' => 'session',4 'session' => 'multitenancy.{tenancy}'5 ],6]
When providing a session 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}
.
You can also use {Tenancy}
and {Resolver}
which will run the value through ucfirst()
to uppercase the first letter, as well as dot notation.
Using
To make use of this identity resolver, once configured, either set the default resolver to its name.
1'defaults' => [2 //...3 'resolver' => 'session',4]
Or provide its name as the second argument when registering tenanted routes.
1Route::tenanted(function () {2 // Define tenant routes here3}, 'session');
The identity resolver itself is the Sprout\Http\Resolvers\SessionIdentityResolver
class
and has a few additional methods that may be of use.
1public function getSessionName(): string2 3public function getRequestSessionName(Tenancy $tenancy): string
The getSessionName()
method will return the config value session
,
and the getRequestSessionName()
will return the session name for the current tenant of the provided tenancy.