Laravel dynamic injection dependency based on request
Hello,
I'm building an application which is deployed each year and we keep the last five years online and available : myapp.com/2018, myapp.com/2019, etc.
Each "year version" may be identical to the previous year or use different new features developped in between. Ex: 2018 version allows registration only with login/password, and 2019 adds Google SSO. Depending on the new feature, it should also be deployed on previous active years or only be proposed from a specific year (like a new tax calculation formula coming into effect only from a certain year).
So we have a kind of big API which developments have to be retro-compatible with the previous years and be modulable. I was thinking of using injection dependency to easily handle the switch between multiple behaviors, with the deployed clients. They would send their "configuration" with the AJAX calls v(i.e. on login, then stored in session) and based on it, we would bind the correct dependencies.
Ex:
Client_1 sent in the AJAX login call :
year : 2019
exportFormat : xslx
useSSO : false
Client_2 sent in the AJAX login call :
year : 2020
exportFormat : pdf
useSSO : true
Then in Laravel, we would have something like :
// AppServiceProvider
$this->app->bind(AuthenticatorInterface::class, function () {
// Resolve dependency...
$useSSO = ... (retrieve the config from session or somewhere);
if($useSSO) {
return new SSOAuthenticator();
} else {
return new LoginPasswordAuthenticator();
}
});
// and so on for other feature (taxCalculator, fileFormater, etc).
My questions are :
- Would it works ? It is a clean and good solution?
- Each year version is linked to its own database, how should we dynamically switch from one to the other based on the config data sent by the client? I have not seen a method to change the default connection, so we would have to use
DB::connection($theYear)->select(..)everywhere (or in a mutualized service) ? - We use API token authentication with Laravel. So, would session storage works ?
Thank you !
Please or to participate in this conversation.