Date: March 2026
Issue: AuthController error when changing YII_ENV from 'dev' to 'prod'
Location: src/Auth/Trait/Oauth2.php
When switching the .env file's YII_ENV setting from dev to prod, the application threw an error related to the AuthController. The root cause was in the initializeOauth2IdentityProviderDualUrls() method within the Oauth2 trait.
In src/Auth/Trait/Oauth2.php, the initializeOauth2IdentityProviderDualUrls() method had a variable scope issue:
private function initializeOauth2IdentityProviderDualUrls(): void
{
if ($this->sR->getEnv() == 'dev') {
$authChoice = AuthChoice::widget();
$developerSandboxHmrc = $authChoice->getClient('developersandboxhmrc');
/** @psalm-var \App\Auth\Client\DeveloperSandboxHmrc $developerSandboxHmrc */
$developerSandboxHmrc->setEnvironment('dev');
} else {
/** @psalm-var \App\Auth\Client\DeveloperSandboxHmrc $developerSandboxHmrc */
$developerSandboxHmrc->setEnvironment('prod'); // ❌ Variable undefined!
}
}The Issue:
- When
YII_ENV=dev: The code initialized$authChoiceand$developerSandboxHmrcwithin the if block - When
YII_ENV=prod: The else block attempted to use$developerSandboxHmrcwithout initializing it first, resulting in an undefined variable error
The fix moved the initialization of $authChoice and $developerSandboxHmrc outside the conditional block, making them available to both branches:
private function initializeOauth2IdentityProviderDualUrls(): void
{
$authChoice = AuthChoice::widget();
$developerSandboxHmrc = $authChoice->getClient('developersandboxhmrc');
/** @psalm-var \App\Auth\Client\DeveloperSandboxHmrc $developerSandboxHmrc */
if ($this->sR->getEnv() == 'dev') {
$developerSandboxHmrc->setEnvironment('dev');
} else {
$developerSandboxHmrc->setEnvironment('prod'); // ✅ Variable now defined!
}
}- Before: Switching to production environment (
YII_ENV=prod) caused AuthController to fail - After: Application works correctly in both development and production environments
To verify the fix:
- Set
YII_ENV=devin.envfile - application should work - Set
YII_ENV=prodin.envfile - application should work without errors - Verify OAuth2 authentication flows function correctly in both environments
src/Auth/Trait/Oauth2.php- Contains the fixsrc/Auth/Controller/AuthController.php- Uses the Oauth2 traitsrc/Auth/Controller/SignupController.php- Also uses the Oauth2 trait.env- Environment configuration file
This method is called during controller initialization and is responsible for configuring the Developer Sandbox HMRC OAuth2 client based on the current environment. The HMRC client requires different URLs for sandbox (dev) versus production environments.