## 1.1.1 在站點根目錄創建composer.json文件,并輸入如下內容
```
{
"require": {
}
}
```
## 1.1.2 通過命令行進入到項目根目錄并執行如下命令
```
composer update
```
然后lara文件夾下回自動生成自動加載文件。目錄結構和文件如圖

## 1.2.1添加路由組件
1. 登錄到`composer`官網,網址是`https://getcomposer.org/`
2. 選擇`Browse Packages` ,或者直接訪問網址`https://packagist.org/`
3. `packagist` 是`composer`工具的主要資源包管理庫,在搜索框中輸入`route`
4. 其中可以看到組件名為`illuminate/routing`,單擊可以查看關于該組件的詳細信息。這里準備用的路由就是他,里邊有添加該組件的方法,
```
composer require illuminate/routing
```
在命令行執行該命令,就會開始下載,請耐心等待。
你會發現其中安裝包含很多其他的組件
```
- Installing symfony/routing (v4.2.1): Loading from cache
- Installing symfony/polyfill-ctype (v1.10.0): Loading from cache
- Installing symfony/polyfill-mbstring (v1.10.0): Loading from cache
- Installing symfony/http-foundation (v4.2.1): Loading from cache
- Installing symfony/contracts (v1.0.2): Loading from cache
- Installing symfony/event-dispatcher (v4.2.1): Loading from cache
- Installing psr/log (1.1.0): Loading from cache
- Installing symfony/debug (v4.2.1): Loading from cache
- Installing symfony/http-kernel (v4.2.1): Loading from cache
- Installing symfony/translation (v4.2.1): Loading from cache
- Installing nesbot/carbon (1.36.1): Loading from cache
- Installing psr/simple-cache (1.0.1): Loading from cache
- Installing psr/container (1.0.0): Loading from cache
- Installing illuminate/contracts (5.7.19): Downloading (100%)
- Installing doctrine/inflector (v1.3.0): Loading from cache
- Installing illuminate/support (5.7.19): Downloading (100%)
- Installing symfony/finder (v4.2.1): Loading from cache
- Installing illuminate/filesystem (5.7.19): Downloading (100%)
- Installing illuminate/session (5.7.19): Downloading (100%)
- Installing illuminate/pipeline (5.7.19): Downloading (100%)
- Installing illuminate/http (5.7.19): Downloading (100%)
- Installing illuminate/container (5.7.19): Downloading (100%)
- Installing illuminate/routing (5.7.19): Downloading (100%)
```
5. 還需要添加一個illuminate/events事件注冊組件,安裝方式如下
```
composer require illuminate/events
```
完成后,如下圖所示

6. 添加路由文件
`lara/routes/web.php`
```
<?php
$app['router']->get('/',function (){
return '<h1>路由成功!!!</h1>';
});
```
7. 添加路口文件
`lara/public/index.php`
```
<?php
//調用自動加載文件,添加自動加載文件函數
require __DIR__.'/../vendor/autoload.php';
//實例化服務器容器,注冊事件、路由服務提供者
$app = new Illuminate\Container\Container;
with(new Illuminate\Events\EventServiceProvider($app))->register();
with(new Illuminate\Routing\RoutingServiceProvider($app))->register();
//加載路由
require __DIR__.'/../routes/web.php';
//實例化請求并分發處理請求
$request = Illuminate\Http\Request::createFromGlobals();
$response = $app['router']->dispatch($request);
//返回請求響應
$response->send();
```
以下是相關說明,非構建代碼;
發現with方法是`lara/vendor/illuminate/support/helpers.php`里定義的方法,
```
if (! function_exists('with')) {
/**
* Return the given value, optionally passed through the given callback.
*
* @param mixed $value
* @param callable|null $callback
* @return mixed
*/
function with($value, callable $callback = null)
{
return is_null($callback) ? $value : $callback($value);
}
}
```
但是這個`helpers.php`是在什么時候載入的呢?
通過查找,是在第一行中的自動加載里加載的,順序如下
1. `require __DIR__.'/../vendor/autoload.php';`
2. `require_once __DIR__ . '/composer/autoload_real.php';`
3. `$includeFiles = require __DIR__ . '/autoload_files.php';`
4. `'72579e7bd17821bb1321b87411366eae' => $vendorDir . '/illuminate/support/helpers.php',`
8. 當添加完這兩個文件后,通過訪問該站點,輸出如下
