## 1.1.1 在站点根目录创建composer.json文件,并输入如下内容
```
{
"require": {
}
}
```
## 1.1.2 通过命令行进入到项目根目录并执行如下命令
```
composer update
```
然后lara文件夹下回自动生成自动加载文件。目录结构和文件如图
![](https://box.kancloud.cn/622b4351b4ff48c02e3c103fa09f2eee_475x306.png)
## 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
```
完成后,如下图所示
![](https://box.kancloud.cn/39b909bbd6f5d63dfea47e7dfca39cde_183x351.png)
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. 当添加完这两个文件后,通过访问该站点,输出如下
![](https://box.kancloud.cn/d63d091430fadc50bdd530495c7bf81f_446x114.png)