Laravel源码解析之路由的使用和示例详解
时间:2022-07-27
本文章向大家介绍Laravel源码解析之路由的使用和示例详解,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
前言
我的解析文章并非深层次多领域的解析攻略。但是参考着开发文档看此类文章会让你在日常开发中更上一层楼。
废话不多说,我们开始本章的讲解。
入口
Laravel启动后,会先加载服务提供者、中间件等组件,在查找路由之前因为我们使用的是门面,所以先要查到Route的实体类。
注册
第一步当然还是通过服务提供者,因为这是laravel启动的关键,在 RouteServiceProvider
内加载路由文件。
protected function mapApiRoutes()
{
Route::prefix('api')
- middleware('api')
- namespace($this- namespace) // 设置所处命名空间
- group(base_path('routes/api.php')); //所得路由文件绝对路径
}
首先require是不可缺少的。因路由文件中没有命名空间。 IlluminateRoutingRouter
下方法
protected function loadRoutes($routes)
{
if ($routes instanceof Closure) {
$routes($this);
} else {
$router = $this;
require $routes;
}
}
随后通过路由找到指定方法,依旧是 IlluminateRoutingRouter
内有你所使用的所有路由相关方法,例如get、post、put、patch等等,他们都调用了统一的方法 addRoute
public function addRoute($methods, $uri, $action)
{
return $this- routes- add($this- createRoute($methods, $uri, $action));
}
之后通过 IlluminateRoutingRouteCollection
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;
}
添加后的结果如下图所示
实例化
依旧通过反射加载路由指定的控制器,这个时候build的参数$concrete = AppApiControllersXxxController
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访问指定方法,一般控制器都会继承父类 IlluminateRoutingController
,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);
}
致谢
感谢你看到这里,本篇文章源码解析靠个人理解。如有出入请拍砖。
以上就是本文的全部内容,希望对大家的学习有所帮助。
- golang(Go语言) byte/[]byte 与 二进制形式字符串 互转
- Sublime Text 最新注册码分享
- Lua table之弱引用
- 看吧,这就是现代化 PHP 该有的样子
- 从web图片裁剪出发:了解H5中的Blob
- Android子线程更新UI主线程方法之Handler
- Drawable.Bitmap.Canvas.Paint.Matrix
- 关于JSON.stringify和Unicode编码,需要注意的几点
- 用 PHP 的方式实现的各类算法合集
- Nginx 反向代理解决前后端联调跨域问题
- JavaScript对象length
- Go1.8.4和Go1.9.1版本发布
- Javascript数组操作
- Tensorflow官方语音识别入门教程 | 附Google新语音指令数据集
- php概述
- php教程
- php环境搭建
- PHP书写格式
- php变量
- php常量
- PHP注释
- php数组
- php字符串 string
- PHP整型 integer
- PHP浮点型 float
- php布尔型
- php数据类型之数组
- php数据类型之对象
- php数据类型之null
- php数据类型之间的转换
- php运算符
- php表达式
- PHP循环控制
- PHP流程控制
- php函数
- php全局变量
- PHP魔术变量
- php命名空间
- php 日期
- PHP包含文件
- php文件
- PHP 文件上传
- php Cookies
- php Sessions
- php email
- php安全email
- php错误处理
- PHP异常处理
- php过滤器
- PHP 高级过滤器
- php json
- php 表单
- PHP MySQL 简介
- PHP 连接 MySQL
- php创建数据库
- php 创建表
- php mysq 插入数据
- PHP MySQL 插入多条数据
- PHP MySQL 预处理语句
- php mysql 读取数据
- php mysql where
- PHP MySQL Order By
- PHP MySQL Update
- PHP MySQL Delete
- php ODBC
- [译]按功能(特性)分包
- Spring Boot Admin简介及实践
- Spring Boot Admin实现服务健康预警
- 除了FastJson,你也应该了解一下Jackson(二)
- 除了FastJson,你也应该了解一下Jackson(一)
- JavaScript性能优化总结
- Spring Boot2+Resilience4j实现容错之Bulkhead
- [译]高性能缓存库Caffeine介绍及实践
- 云原生时代高性能Java框架—Quarkus(一)
- 云原生时代高性能Java框架—Quarkus(二)
- 使用TypeScript积累自己的类库
- 在Egret项目中使用protobuf
- JavaScript原型与继承
- 当TKE遇上Coding, 业务如何实现快速迭
- cocos creator 1.x透明渐变效果实现