yii2: Failed to instantiate component or class "cache".
What steps will reproduce the problem?
Since version 2.0.33 the urlManager will throw an exception if cache component is not properly configured.
Failed to instantiate component or class "cache".
Now the out of the box setup process is disturbed i would say. Assuming the following config:
return [
'id' => 'luya-test-case',
'basePath' => dirname(__DIR__),
'components' => [
'urlManager' => [
'rules' => [
// my rules goes here
]
],
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'sqlite::memory:',
],
];
Before version 2.0.33 it was possible to run this code above, but since version 2.0.33 if have to either setup cache component (for example using DummyCache) or set urlManager::$cache to null.
- In order to be strict in terms of configuration would either recommend to set urlManager::$cache to null by default (which requires people to take action to enable caching, which seems to be unpractical as well). (https://github.com/yiisoft/yii2/blob/master/framework/web/UrlManager.php#L135)
- Or define DummyCache in coreComponents (https://github.com/yiisoft/yii2/blob/master/framework/base/Application.php#L624)
- Or just revert that commit: https://github.com/yiisoft/yii2/pull/17890
Additional info
| Q | A |
|---|---|
| Yii version | 2.0.33 |
| PHP version | all |
| Operating system | all |
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 24 (23 by maintainers)
Commits related to this issue
- Fix #17933: Use DummyCache by default — committed to yiisoft/yii2 by samdark 4 years ago
- Bug #17933: Log warning instead of erroring when URLManager is unable to initialize cache — committed to yiisoft/yii2 by samdark 4 years ago
- set cache=null in urlManager Starting with Yii 2.0.33, urlManager must have a specified cache. Setting to null to silence warning logged for every access. https://github.com/yiisoft/yii2/issues/17933 — committed to silinternational/idp-id-broker by briskt 4 years ago
Logging a warning sounds like a compromise but it would spam logs a lot in @nadar case.
Because it will introduce performance regression, which is the worse regression possible, since it will be hard to notice. It is also not pragmatic - in most cases you have cache configured and you want to use it in UrlManager. Optimizing default configuration for an edge case does not seems to like Yii-way.
Converting exception to
Yii::warning()should be BC and still give you a clue that your configuration is not optimal.Yeah, right, nevermind then. Also not really fix for this issue. I did not read everything before responding.
Actually I’d vote to set
UrlManager::$cachetonullby default and add an upgrade note to 2.0.34.