qiuyimo / qiuyuhome.github.io

我的博客, 记录一些技术知识和问题的解决方法. 主要涉及到了 PHP, MySQL, Linux, JavaScript, HTML, Docker等等.
http://www.qiuyuhome.com
MIT License
1 stars 2 forks source link

[2018-04-06 22:41:52] 阅读 laravel 源码 #12

Open qiuyimo opened 6 years ago

qiuyimo commented 6 years ago

[2018-04-06 22:41:52]

阅读 laravel 源码

qiuyimo commented 6 years ago

遇到不熟悉的 PHP 自带的函数和常量

qiuyimo commented 6 years ago

源码理解

一. 入口文件. 首先会引入 composer 的自动加载机制.

二. 引入 /bootstrap/app.php 文件.

这个文件有三部分组成. 分开来说.

创建 服务容器.

/*
|--------------------------------------------------------------------------
| Create The Application
|--------------------------------------------------------------------------
|
| The first thing we will do is create a new Laravel application instance
| which serves as the "glue" for all the components of Laravel, and is
| the IoC container for the system binding all of the various parts.
|
*/

$app = new Illuminate\Foundation\Application(
    realpath(__DIR__.'/../')
);

Illuminate\Foundation\Application 这个类的作用

Illuminate\Foundation\Application 这个类中, 构造方法是:

    /**
     * Create a new Illuminate application instance.
     *
     * @param  string|null  $basePath
     * @return void
     */
    public function __construct($basePath = null)
    {
        if ($basePath) {
            $this->setBasePath($basePath);
        }

        $this->registerBaseBindings();

        $this->registerBaseServiceProviders();

        $this->registerCoreContainerAliases();
    }

从代码中大概可以看出, 做了哪些事情.

绑定重要的接口

/*
|--------------------------------------------------------------------------
| 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.
|
*/

$app->singleton(
    Illuminate\Contracts\Http\Kernel::class,
    App\Http\Kernel::class
);

$app->singleton(
    Illuminate\Contracts\Console\Kernel::class,
    App\Console\Kernel::class
);

$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;