Yii2 类自动加载机制
2019-08-24 14:08:07 9 举报
AI智能生成
Yii2 类自动加载机制
作者其他创作
大纲/内容
Yii::$classMap
'yii\base\Application' => YII2_PATH . '/base/Application.php'
'yii\base\Behavior' => YII2_PATH . '/base/Behavior.php'
'yii\base\Event' => YII2_PATH . '/base/Event.php'
'yii\base\Object' => YII2_PATH . '/base/Object.php'
...
yii\web\Application
Yii::setAlias('@webroot', dirname($request->getScriptFile()));
Yii::setAlias('@web', $request->getBaseUrl());
yii\base\Application
Yii::setAlias('@app', $this->getBasePath());
Yii::setAlias('@runtime', $this->_runtimePath);
Yii::setAlias('@vendor', $this->_vendorPath);
Yii::setAlias('@bower', $this->_vendorPath . DIRECTORY_SEPARATOR . 'bower');
Yii::setAlias('@npm', $this->_vendorPath . DIRECTORY_SEPARATOR . 'npm');
Yii::setAlias($name, $path);
vendor/yiisoft/extensions.php
$vendorDir = dirname(__DIR__);
'@yii/bootstrap' => $vendorDir . '/yiisoft/yii2-bootstrap'
'@yii/gii' => $vendorDir . '/yiisoft/yii2-gii'
'@yii/faker' => $vendorDir . '/yiisoft/yii2-faker'
'@yii/swiftmailer' => $vendorDir . '/yiisoft/yii2-swiftmailer'
'@callmez/wechat/sdk' => $vendorDir . '/callmez/yii2-wechat-sdk'
'@yii/debug' => $vendorDir . '/yiisoft/yii2-debug'
common/config/bootstrap.php
Yii::setAlias('@common', dirname(__DIR__));
Yii::setAlias('@admin', dirname(dirname(__DIR__)) . '/admin');
Yii::setAlias('@console', dirname(dirname(__DIR__)) . '/console');
index.php
require __DIR__ . '/../../vendor/autoload.php';
require __DIR__ . '/../../vendor/yiisoft/yii2/Yii.php';
vendor/yiisoft/yii2/Yii.php
require(__DIR__ . '/BaseYii.php');
class Yii extends \yii\BaseYii{}
spl_autoload_register(['Yii', 'autoload'], true, true);
Yii::$classMap = require(__DIR__ . '/classes.php');
Yii::$container = new yii\di\Container();
yii\BaseYii
defined('YII2_PATH') or define('YII2_PATH', __DIR__);
其他define
public static $classMap = [];
public static $app;
public static $aliases = ['@yii' => __DIR__];
public static $container;
public static function getVersion()
public static function autoload($className)
if (isset(static::$classMap[$className]))
$classFile = static::$classMap[$className]
if ($classFile[0] === '@')
$classFile = static::getAlias($classFile);
elseif (strpos($className, '\\') !== false)
$classFile = static::getAlias('@' . str_replace('\\', '/', $className) . '.php', false);
include($classFile);
public static function getAlias($alias, $throwException = true)
public static function getRootAlias($alias)
public static function setAlias($alias, $path)
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
其他方法
public static function getLogger()
public static function setLogger($logger)
public static function trace($message, $category = 'application')
public static function error($message, $category = 'application')
public static function warning($message, $category = 'application')
public static function info($message, $category = 'application')
public static function beginProfile($token, $category = 'application')
public static function endProfile($token, $category = 'application')
public static function powered()
public static function t($category, $message, $params = [], $language = null)
public static function getObjectVars($object)
return get_object_vars($object);
vendor/autoload.php
Composer/PSR4支持
require_once __DIR__ . '/composer/autoload_real.php'
private static $loader;
public static function loadClassLoader($class)
require __DIR__ . '/ClassLoader.php';
public static function getLoader()
spl_autoload_register(array('ComposerAutoloaderInit随机字符', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader();
::getLoader()
0 条评论
下一页