Laravel源码解析之路由的使用和示例详解


Posted in PHP onSeptember 27, 2018

前言

我的解析文章并非深层次多领域的解析攻略。但是参考着开发文档看此类文章会让你在日常开发中更上一层楼。

废话不多说,我们开始本章的讲解。

入口

Laravel启动后,会先加载服务提供者、中间件等组件,在查找路由之前因为我们使用的是门面,所以先要查到Route的实体类。

注册

第一步当然还是通过服务提供者,因为这是laravel启动的关键,在 RouteServiceProvider 内加载路由文件。

protected function mapApiRoutes()
{
  Route::prefix('api')
     ->middleware('api')
     ->namespace($this->namespace) // 设置所处命名空间
     ->group(base_path('routes/api.php')); //所得路由文件绝对路径
}

首先require是不可缺少的。因路由文件中没有命名空间。 Illuminate\Routing\Router 下方法

protected function loadRoutes($routes)
{
  if ($routes instanceof Closure) {
    $routes($this);
  } else {
    $router = $this;

    require $routes;
  }
}

随后通过路由找到指定方法,依旧是 Illuminate\Routing\Router 内有你所使用的所有路由相关方法,例如get、post、put、patch等等,他们都调用了统一的方法 addRoute

public function addRoute($methods, $uri, $action)
{
  return $this->routes->add($this->createRoute($methods, $uri, $action));
}

之后通过 Illuminate\Routing\RouteCollection addToCollections 方法添加到集合中

protected function addToCollections($route)
{
  $domainAndUri = $route->getDomain().$route->uri();

  foreach ($route->methods() as $method) {
    $this->routes[$method][$domainAndUri] = $route;
  }

  $this->allRoutes[$method.$domainAndUri] = $route;
}

添加后的结果如下图所示

Laravel源码解析之路由的使用和示例详解

实例化

依旧通过反射加载路由指定的控制器,这个时候build的参数$concrete = App\Api\Controllers\XxxController

public function build($concrete)
{
  // If the concrete type is actually a Closure, we will just execute it and
  // hand back the results of the functions, which allows functions to be
  // used as resolvers for more fine-tuned resolution of these objects.
  if ($concrete instanceof Closure) {
    return $concrete($this, $this->getLastParameterOverride());
  }
  
  $reflector = new ReflectionClass($concrete);
  // If the type is not instantiable, the developer is attempting to resolve
  // an abstract type such as an Interface of Abstract Class and there is
  // no binding registered for the abstractions so we need to bail out.
  if (! $reflector->isInstantiable()) {
    return $this->notInstantiable($concrete);
  }
  
    
  $this->buildStack[] = $concrete;

  $constructor = $reflector->getConstructor();
  // If there are no constructors, that means there are no dependencies then
  // we can just resolve the instances of the objects right away, without
  // resolving any other types or dependencies out of these containers.
  if (is_null($constructor)) {
  
      array_pop($this->buildStack);
  
      return new $concrete;
  }

  $dependencies = $constructor->getParameters();
  // Once we have all the constructor's parameters we can create each of the
  // dependency instances and then use the reflection instances to make a
  // new instance of this class, injecting the created dependencies in.
  $instances = $this->resolveDependencies(
    $dependencies
  );

  array_pop($this->buildStack);
  
  return $reflector->newInstanceArgs($instances);
}

这时将返回控制器的实例,下面将通过url访问指定方法,一般控制器都会继承父类 Illuminate\Routing\Controller ,laravel为其设置了别名 BaseController

public function dispatch(Route $route, $controller, $method)
{
  
  $parameters = $this->resolveClassMethodDependencies(
    $route->parametersWithoutNulls(), $controller, $method
  );

  if (method_exists($controller, 'callAction')) {

      return $controller->callAction($method, $parameters);
  }
    
  return $controller->{$method}(...array_values($parameters));
}

Laravel通过controller继承的callAction去调用子类的指定方法,也就是我们希望调用的自定义方法。

public function callAction($method, $parameters)
{
  return call_user_func_array([$this, $method], $parameters);
}

致谢

感谢你看到这里,本篇文章源码解析靠个人理解。如有出入请拍砖。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

PHP 相关文章推荐
php中的MVC模式运用技巧
May 03 PHP
php入门小知识
Mar 24 PHP
PHP 开源AJAX框架14种
Aug 24 PHP
PHPnow安装服务[apache_pn]失败的问题的解决方法
Sep 10 PHP
PHP中获取变量的变量名的一段代码的bug分析
Jul 07 PHP
PHP中数组的分组排序实例
Jun 01 PHP
自己写的兼容低于PHP 5.5版本的array_column()函数
Oct 24 PHP
PHP中通过fopen()函数访问远程文件示例
Nov 18 PHP
Zend Framework实现多文件上传功能实例
Mar 21 PHP
详解PHP文件的自动加载(autoloading)
Feb 04 PHP
PHP+ajax实现二级联动菜单功能示例
Aug 10 PHP
PHP如何开启Opcache功能提升程序处理效率
Apr 27 PHP
php实现有序数组旋转后寻找最小值方法
Sep 27 #PHP
PHP实现SMTP邮件的发送实例
Sep 27 #PHP
ThinkPHP like模糊查询,like多匹配查询,between查询,in查询,一般查询书写方法
Sep 26 #PHP
thinkPHP利用ajax异步上传图片并显示、删除的示例
Sep 26 #PHP
多个Laravel项目如何共用migrations详解
Sep 25 #PHP
php中上传文件的的解决方案
Sep 25 #PHP
PHP调用微博接口实现微博登录的方法示例
Sep 22 #PHP
You might like
常见的PHP五种设计模式小结
2011/03/23 PHP
laravel5.4生成验证码的实例讲解
2017/08/05 PHP
ThinkPHP3.2.3框架邮件发送功能图文实例详解
2019/04/23 PHP
如何用javascript去掉字符串里的所有空格
2007/02/08 Javascript
Javascript 匿名函数及其代码模式原理
2010/03/19 Javascript
JavaScript 选中文字并响应获取的实现代码
2011/08/28 Javascript
js判断浏览器类型的方法
2013/08/07 Javascript
JavaScript图片放大技术(放大镜)实现代码分享
2013/11/14 Javascript
Angular实现form自动布局
2016/01/28 Javascript
javascript基础语法——全面理解变量和标识符
2016/06/02 Javascript
jQuery实现页面点击后退弹出提示框的方法
2016/08/24 Javascript
JS中事件冒泡和事件捕获介绍
2016/12/13 Javascript
node跨域请求方法小结
2017/08/25 Javascript
javascript 中事件冒泡和事件捕获机制的详解
2017/09/01 Javascript
详解JavaScript 为什么要有 Symbol 类型?
2019/04/03 Javascript
详解如何在Vue项目中发送jsonp请求
2019/10/25 Javascript
vue中echarts图表大小适应窗口大小且不需要刷新案例
2020/07/19 Javascript
jquery插件实现轮播图效果
2020/10/19 jQuery
Python 序列的方法总结
2016/10/18 Python
Python实现的基于优先等级分配糖果问题算法示例
2018/04/25 Python
Python实现矩阵相乘的三种方法小结
2018/07/26 Python
使用tensorflow实现线性svm
2018/09/07 Python
Django中使用session保持用户登陆连接的例子
2019/08/06 Python
Flask框架学习笔记之表单基础介绍与表单提交方式
2019/08/12 Python
Django实现文件上传下载
2019/10/06 Python
Python自动创建Excel并获取内容
2020/09/16 Python
Python绘图实现台风路径可视化代码实例
2020/10/23 Python
Python模拟登录和登录跳转的参考示例
2020/10/30 Python
工程售后服务方案
2014/06/08 职场文书
2014年企业团支部工作总结
2014/12/10 职场文书
失职检讨书大全
2015/01/26 职场文书
2015年重阳节主持词
2015/07/04 职场文书
2016国庆节活动宣传语
2015/11/25 职场文书
2016教师年度考核评语大全
2015/12/01 职场文书
pytorch损失反向传播后梯度为none的问题
2021/05/12 Python
画错魏国疆域啦!《派对咖孔明》动画因作画失误于官网致歉
2022/04/07 日漫