Laravel 4 入门三讲(下)laravel 在注册路由后还执行了哪些操作?


  上篇介绍了 Laravel 的路由注册机制“Laravel 4 的路由是如何完成注册的?”作为这个系列的最后一讲,本篇将要为大家介绍的,是执行了 $app->run() 之后所发生的事,以及这个过程中涉及到的应用程序事件。

  在完成了路由的注册后,紧接着执行的就是 public/index.php 中的 $app->run(); 让我们来看一下 run 方法的源码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public function run()
{
     // 调度路由返回响应实例
     $response = $this ->dispatch( $this [ 'request' ]);
 
     // 执行 close 应用程序事件
     $this [ 'router' ]->callCloseFilter( $this [ 'request' ], $response );
 
     // 发送响应
     $response ->send();
 
     // 执行 finish 应用程序事件
     $this [ 'router' ]->callFinishFilter( $this [ 'request' ], $response );
}

  下面是整个流程的分析:

  1. 第4行,执行了路由调度操作 dispatch
    1. 执行应用程序事件 App::before(function($request){})
    2. 执行 Illuminate\Routing\Route.php 的 run 方法
      1. 执行 Route::filter($route, $request) 前置操作
      2. 最终业务回调,取得响应实例(这里执行了之前注册进路由的回调函数)
      3. 执行 Route::filter($route, $request, $response) 后置操作
    3. 执行应用程序事件 App::after(function($request, $response){})
  2. 第7行,执行应用程序事件 App::close(function($request, $response){})
  3. 第10行,发送 1-2-2 中获取的响应实例
  4. 第13行,执行应用程序事件 App::finish(function($request, $response){})
  5. 最后在 public/index.php 中的 $app->shutdown() 时,执行最后的应用程序事件 App::shutdown(function($application){})

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值