Skip to content

引入容器类,

php
$app = require_once __DIR__.'/../bootstrap/app.php';

查看引入代码,

php
// 创建容器,传入环境设置的目录或者当前目录
$app = new Illuminate\Foundation\Application(
    $_ENV['APP_BASE_PATH'] ?? dirname(__DIR__)
);

/*
|--------------------------------------------------------------------------
| Bind Important Interfaces
|--------------------------------------------------------------------------
|
| Next, we need to bind some important interfaces into the container so
| we will be able to resolve them when needed. The kernels serve the
| incoming requests to this application from both the web and CLI.
|
*/

/**
 * 给容器绑定接口和实现类Kernel
 * 该类处理HTTP请求
 */ 
$app->singleton(
    Illuminate\Contracts\Http\Kernel::class,
    App\Http\Kernel::class
);
/**
 * 给容器绑定接口和实现类Kernel
 * 该类处理命令行请求
 */
$app->singleton(
    Illuminate\Contracts\Console\Kernel::class,
    App\Console\Kernel::class
);
/**
 * 给容器绑定接口和实现类Handler
 * 该类处理异常
 */ 
$app->singleton(
    Illuminate\Contracts\Debug\ExceptionHandler::class,
    App\Exceptions\Handler::class
);

/*
|--------------------------------------------------------------------------
| Return The Application
|--------------------------------------------------------------------------
|
| This script returns the application instance. The instance is given to
| the calling script so we can separate the building of the instances
| from the actual running of the application and sending responses.
|
*/

return $app;

Application类源码

new时调用构造函数,

php
public function __construct($basePath = null)
{
    // 设置基础目录路径
    if ($basePath) {
        $this->setBasePath($basePath);
    }
    // 注册基础绑定
    $this->registerBaseBindings();
    // 注册服务提供者
    $this->registerBaseServiceProviders();
    // 注册核心别名类
    $this->registerCoreContainerAliases();
}