Right now I'm trying to implement themming for my Yii2 based project. How I see the thing now:
- User chooses an application theme from the list on the settings page in backend.
- Using yii2-settings I'm saving all the configuration data in DB (pretty easy).
- In the application bootstrap.php I'm creating new alias called @theme. Basically it should lead us to a application theme base path (used in search paths, assets manager, e.t.c.).
According to official documentation, that's how I configured my view component:
'view' => [ 'theme' => [ 'basePath' => '@theme', 'baseUrl' => '@theme', 'pathMap' => [ '@app/views' => '@theme', '@app/widgets' => '@theme/widgets', '@app/modules' => '@theme/modules', ], ], ],
An issue I have is with p.3. According to yii2-settings documentation that's how I supposed to read the settings:
$theme = Yii::$app->settings->get('name', 'general');
Yii::setAlias('@theme', realpath(dirname(__FILE__)."/../../themes/$theme"));
But obviously, it's not working for me because of yii2-settings component didn't initialized yet when bootstrap.php is called. I've been trying to initialize it later in the init() method of my base controller, then adjust other aliases manually, but I feel that way being somewhat 'unclean', and also it still fails because of @theme alias is also used in asset file which is Yii2 starting to publish before calling the controller's init method.
So does anyone has any thoughts of how to do that 'hacking' the code as less as possible? I know I could just move configuration to some file, then read it manually before the application initialization, but it's still not the way I want to go. Maybe there's some way to override some system component to set the alias after db component is loaded, but before view component configuration? Or Yii loads this components in a different order? Anyway. Any help would be appreciated!