Api.php 1.1 KB

123456789101112131415161718192021222324252627282930
  1. <?php
  2. namespace frontend\modules\api;
  3. use Yii;
  4. class Api extends \yii\base\Module
  5. {
  6. public $controllerNamespace = 'frontend\modules\api\controllers';
  7. public function init()
  8. {
  9. parent::init();
  10. $config = require(__DIR__ . '/config.php');
  11. // 获取应用程序的组件
  12. $components = \Yii::$app->getComponents();
  13. // 遍历子模块独立配置的组件部分,并继承应用程序的组件配置
  14. foreach ($config['components'] as $k => $component) {
  15. if (isset($component['class']) && !isset($components[$k])) continue;
  16. $config['components'][$k] = array_merge($components[$k], $component);
  17. }
  18. // 将新的配置设置到应用程序
  19. // 很多都是写 Yii::configure($this, $config),但是并不适用子模块,必须写 Yii::$app
  20. Yii::configure(Yii::$app, $config);
  21. //由于yii框架在之前已经注册过errorHandler,所以这里需要重新注册一下才能生效
  22. Yii::$app->errorHandler->register();
  23. // custom initialization code goes here
  24. }
  25. }