Yii2 配置项
2019-08-24 19:18:16 6 举报
AI智能生成
Yii2 配置项
作者其他创作
大纲/内容
index.php
$config = yii\helpers\ArrayHelper::merge(
require(__DIR__ . '/../../common/config/main.php'),
require(__DIR__ . '/../../common/config/main-prod.php'),
require(__DIR__ . '/../config/main.php'),
require(__DIR__ . '/../config/main-prod.php')
);
require(__DIR__ . '/../../common/config/main.php'),
require(__DIR__ . '/../../common/config/main-prod.php'),
require(__DIR__ . '/../config/main.php'),
require(__DIR__ . '/../config/main-prod.php')
);
$application = new yii\web\Application($config);
$application->run();
Yii
yii\BaseYii
public static $app;
public static function createObject($type, array $params = [])
if (is_string($type))
static::$container->get($type, $params)
elseif (is_array($type) && isset($type['class']))
$class = $type['class']
static::$container->get($class, $params, $type)
elseif (is_callable($type, true))
static::$container->invoke($type, $params)
public static function configure($object, $properties)
foreach ($properties as $name => $value)
$object->$name = $value
yii\web\Application
public function coreComponents()
return array_merge(parent::coreComponents(), [
'request' => ['class' => 'yii\web\Request'],
'response' => ['class' => 'yii\web\Response'],
'session' => ['class' => 'yii\web\Session'],
'user' => ['class' => 'yii\web\User'],
'errorHandler' => ['class' => 'yii\web\ErrorHandler'],
]);
'request' => ['class' => 'yii\web\Request'],
'response' => ['class' => 'yii\web\Response'],
'session' => ['class' => 'yii\web\Session'],
'user' => ['class' => 'yii\web\User'],
'errorHandler' => ['class' => 'yii\web\ErrorHandler'],
]);
yii\base\Module
private $_basePath;
public static function setInstance($instance)
if ($instance === null)
unset(Yii::$app->loadedModules[get_called_class()]);
else
Yii::$app->loadedModules[get_class($instance)] = $instance;
public function setBasePath($path)
$path = Yii::getAlias($path);
$p = strncmp($path, 'phar://', 7) === 0 ? $path : realpath($path);
$this->_basePath = $p;
public function getBasePath()
public function getModule($id, $load = true)
yii\di\ServiceLocator
public function has($id, $checkInstance = false)
$checkInstance ? isset($this->_components[$id]) : isset($this->_definitions[$id]);
public function get($id, $throwException = true)
public function set($id, $definition)
yii\base\Component
public function __set($name, $value)
public function __get($name)
yii\base\Object
public function __construct($config = [])
Yii::configure($this, $config);
$this->init();
yii\base\ErrorHandler
public function register()
private $_memoryReserve;
ini_set('display_errors', false);
et_exception_handler([$this, 'handleException']);
if (defined('HHVM_VERSION'))
set_error_handler([$this, 'handleHhvmError']);
else
set_error_handler([$this, 'handleError']);
if ($this->memoryReserveSize > 0)
$this->_memoryReserve = str_repeat('x', $this->memoryReserveSize);
register_shutdown_function([$this, 'handleFatalError']);
yii\base\Application
public $extensions;
public $bootstrap = [];
config配置
public $state;
public $loadedModules = [];
public function __construct($config = [])
Yii::$app = $this;
static::setInstance($this);
$this->state = self::STATE_BEGIN;
$this->preInit($config);
$this->registerErrorHandler($config);
Component::__construct($config);
public function preInit(&$config)
$this->setBasePath($config['basePath']);
$this->setVendorPath($config['vendorPath']);
$this->setRuntimePath($config['runtimePath']);
$this->setTimeZone($config['timeZone']);
$this->setContainer($config['container']);
foreach ($this->coreComponents() as $id => $component)
if (!isset($config['components'][$id]))
$config['components'][$id] = $component;
elseif (is_array($config['components'][$id]) && !isset($config['components'][$id]['class']))
$config['components'][$id]['class'] = $component['class'];
public function setBasePath($path)
parent::setBasePath($path);
Yii::setAlias('@app', $this->getBasePath());
public function setVendorPath($path)
$this->_vendorPath = Yii::getAlias($path);
Yii::setAlias('@vendor', $this->_vendorPath);
Yii::setAlias('@bower', $this->_vendorPath . DIRECTORY_SEPARATOR . 'bower');
Yii::setAlias('@npm', $this->_vendorPath . DIRECTORY_SEPARATOR . 'npm');
public function setRuntimePath($path)
$this->_runtimePath = Yii::getAlias($path);
Yii::setAlias('@runtime', $this->_runtimePath);
public function setTimeZone($value)
date_default_timezone_set($value);
public function setContainer($config)
Yii::configure(Yii::$container, $config);
public function coreComponents()
return [
'log' => ['class' => 'yii\log\Dispatcher'],
'view' => ['class' => 'yii\web\View'],
'formatter' => ['class' => 'yii\i18n\Formatter'],
'i18n' => ['class' => 'yii\i18n\I18N'],
'mailer' => ['class' => 'yii\swiftmailer\Mailer'],
'urlManager' => ['class' => 'yii\web\UrlManager'],
'assetManager' => ['class' => 'yii\web\AssetManager'],
'security' => ['class' => 'yii\base\Security'],
];
'log' => ['class' => 'yii\log\Dispatcher'],
'view' => ['class' => 'yii\web\View'],
'formatter' => ['class' => 'yii\i18n\Formatter'],
'i18n' => ['class' => 'yii\i18n\I18N'],
'mailer' => ['class' => 'yii\swiftmailer\Mailer'],
'urlManager' => ['class' => 'yii\web\UrlManager'],
'assetManager' => ['class' => 'yii\web\AssetManager'],
'security' => ['class' => 'yii\base\Security'],
];
protected function registerErrorHandler(&$config)
$this->set('errorHandler', $config['components']['errorHandler']);
unset($config['components']['errorHandler']);
$this->getErrorHandler()->register();
public function getErrorHandler()
$this->get('errorHandler')
public function init()
$this->state = self::STATE_INIT;
$this->bootstrap();
protected function bootstrap()
$file = Yii::getAlias('@vendor/yiisoft/extensions.php');
$this->extensions = is_file($file) ? include($file) : [];
foreach ($this->extensions as $extension)
foreach ($extension['alias'] as $name => $path)
Yii::setAlias($name, $path);
$component = Yii::createObject($extension['bootstrap']);
if ($component instanceof BootstrapInterface)
$component->bootstrap($this);
foreach ($this->bootstrap as $class)
$component = null;
if (is_string($class))
if ($this->has($class))
$component = $this->get($class);
elseif ($this->hasModule($class))
$component = $this->getModule($class);
if (!isset($component))
$component = Yii::createObject($class);
if ($component instanceof BootstrapInterface)
$component->bootstrap($this);
0 条评论
下一页