本篇文章给大家带来的内容是关于laravel框架下路由的使用(源码解析),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。
我的解析文章并非深层次多领域的解析攻略。但是参考着开发文档看此类文章会让你在日常开发中更上一层楼。
废话不多说,我们开始本章的讲解。
laravel启动后,会先加载服务提供者、中间件等组件,在查找路由之前因为我们使用的是门面,所以先要查到route的实体类。
第一步当然还是通过服务提供者,因为这是laravel启动的关键,在routerviceprovider
内加载路由文件。
1
2
3
4
5
6
7
protected
function
mapapiroutes()
{
route::prefix(
'api'
)
->middleware(
'api'
)
->
namespace
(
$this
->
namespace
)
// 设置所处命名空间
->group(ba_path(
'routes/api.php'
));
//所得路五大天王由文件绝对路径
}
首先require是不可缺少的。因路由文件中没有命名空间。illuminate\routing\router
下方法
1
2
3
4
5
6
7
8
9
10
protected
function
loadroutes(
$routes
)
{
if
(
$routes
instanceof
closure) {
$routes
(
$this
);
}
el
{
$router
=
$this
;
require
$routes
;
}
}
随后通过路由找到指定方法,依旧是illuminate\routing\router
内有你所使用的所有路由相关方法,例如get、post、put、patch等等,他们都调用了统一的方法addroute
1
2
3
4
public
function
addroute(
$methods
,
$uri
,
$action
)
{
return
$this
->routes->add(
$this
->createroute(
$methods
,
$uri
,
$action
));
}
之后通过illuminate\routing\routecollection
addtocollections 方法添加到集合中
1
2
3
4
5
6
7
8
9
10
protected
function
addtocollections(
$route
)
{
$domainanduri
=
$route
->getdomain().
$route
->uri();
foreach
(
$route
->methods()
as
$method
) {
$this
->route上海画室s[
$method
][
$domainanduri
] =
$route
;
}
$this
->allroutes[
$method
.
$domainanduri
] =
$route
;
}
添加后的结果如下图所示
通过illuminate\routing\router
方法开始运行路由实例化的逻辑
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
protected
function
runroute(request
$request
, route
$route
)
{
$request
->trouteresolver(
function
()
u
(
$route
) {
return
$route
;
});
$this
->events->disprve的名词atch(
new
events\routematched(
$route
,
$request
));
return
$this
->preparerespon(
$request
,
$this
->runroutewithinstack(
$route
,
$request
)
);
}
....
protected
function
runroutewithinstack(route
$route
, request
$request
)
{
$shouldskipmiddleware
=
$this
->container->bound(
'middleware.disable'
) &&
$this
->container->make(
'middleware.disable'
) === true;
$middleware
=
$shouldskipmiddleware
? [] :
$this
->gatherroutemiddleware(
$route
);
return
(
new
pipeline(
$this
->container))
->nd(
$request
)
->through(
$middleware
)
->then(
function
(
$request
)
u
(
$route
) {
return
$this
->preparerespon(
$request
,
$route
->run()
// 此处调用run方法
);
});
}
在illuminate\routing\route
下 run 方用于执行控制器的方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public
function
run()
{
$this
->container =
$this
->container ?:
new
container;
try
{
if
(
$this
->iscontrolleraction()) {
return
$this
->runcontroller();
//运行一个路由并作出响应
}
return
$this
->runcallable();
}
c财务能力分析atch
(httpresponexception
$e
) {
return
$e
->getrespon();
}
}
从上述方法内可以看出 runcontroller 是运行路由的关键,方法内运行了一个调度程序,将控制器$this->getcontroller()
和控制器方法$this->getcontrollermethod()
传入到dispatch
调度方法内
1
2
3
4
5
6
7
protected
function
runcontroller()
{
return
$this
->controllerdispatcher()->dispatch(
$this
,
$this
->getcontroller(),
$this
->getcontrollermethod()
);
}
这里注意getcontroller()
才是真正的将控制器实例化的方法
1
2
3
4
5
6
7
8
9
10
public
function
getcontroller()
{
if
(!
$this
->controller) {
$class
=
$this
->parcontrollercallback()[0];
// 0=>控制器 xxcontroller 1=>方法名 index
$this
->controller =
$this
->container->make(ltrim(
$class
,
'\\'
));
// 交给容器进行反射
}
return
$this
->controller;
}
依旧通过反射加载路由指定的控制器,情人歌词这个时候build的参数$concrete =app\api\controllers\xxxcontroller
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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
// ud as resolvers for more fine-tuned resolution of the 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 the 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 u 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为其设置了别名 bacontroller
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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去调用子类的指定方法,也就是我们希望调用的自定义方法。
1
2
3
4
public
function
callaction(
$method
,
$parameters
)
{
return
call_ur_func_array([
$this
,
$method
],
$parameters
);
}
本文发布于:2023-04-07 19:45:21,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/e6dfe38a4aa0cb1acfb7f560267dad4e.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:Laravel框架下路由的使用(源码解析).doc
本文 PDF 下载地址:Laravel框架下路由的使用(源码解析).pdf
留言与评论(共有 0 条评论) |