d8入口文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
<?php
/**
* @file
* The PHP page that serves all page requests on a Drupal installation.
*
* All Drupal code is released under the GNU General Public License.
* See <a href="/project/copyright" class="alinks-link" title="模块介绍:可以设定网站整体和单篇内容的著作权/授权声明。管理者可以设定可选用的著作权种类,呈现在区块或页尾里。手册页面(Book)可以选择性地设定所有子页面为同一著作权声明,并让每一份手册或区域有其自己的著作权声明。">COPYRIGHT</a>.txt and LICENSE.txt files in the "core" directory.
*/
//使用了两个命名空间,有什么作用呢,后面auto会用到。
use Drupal\Core\DrupalKernel;
use Symfony\Component\HttpFoundation\Request;
// 包含自动加载文件
$autoloader = require_once
'autoload.php'
;
// 创建一个DrupalKernel对象
$kernel =
new
DrupalKernel(
'prod'
, $autoloader);
//获取请求
$request = Request::createFromGlobals();
//处理对请求处理
$response = $kernel->handle($request);
//向浏览器发出回应
$response->send();
//事件监听,监听整个请求和回应的过程
$kernel->terminate($request, $response);
|
这一整套流程其实就是Symfony的运行机制
当然为了更好的了解这个运行过程,从网上找了个图片下
其实这里可以用自己的理解方式来理解上述问题,我做了如下方式,当然这只是粗糙的思路,并不是完美的代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
//获取http请求并解析请求
$request = [
'http'
=>$_SEVER,
'params'
=>$_GET,
'params'
=>$_POST] ;
//获取路由,路由就是请求路径
$getrouter = $request[
'路由'
];
//定义所有路由
$allrouter = [
'router1'
=>
'file/class/action'
,
'路由2'
=>
'对应文件/对应类/对应方法'
];
//解析路由,返回调用哪个类,和使用那个方法
$router = separaterouter($allrouter[
'$getrouter'
]);
$file =
'file'
;
$
class
=
'class'
;
$action =
'action'
;
/*
*生成回应
*其中action里面可能回使用数据库,
*渲染html,也可能生成json、xml
*或者普通的字符串,总之做了很多事情
*/
require_once($file);
$response =
new
$router.
'()'
;
$page = $response->$action.
'()'
;
echo $page;
//监听请求和回应
watchdog($request,$response);
|
流程就到这里,下面来说说命名空间和autoload
熟悉这个的人就不用看了,5.3以前php的命名空间是不存在的,为啥后来有了
为了是区分两个相同类名,而不是同一个类的情况,比如在颜色的文件下有个green(绿色)类,定义了的色值、调色方法等,在姓氏里面定义了也green(格林)类,定义了历史、常用名等等。这两个在房屋装修中可能都会用到。
扯远了,回来继续
1
2
3
4
5
6
7
8
9
10
11
12
13
|
//看着这行代码
$autoloader = require_once
'autoload.php'
;
//打开文件,后又是一个包含,继续打开
return
require __DIR__ .
'/vendor/autoload.php'
;
//注意composer,这个东西,这是一个php管理工具
<?php
// autoload.php @generated by Composer
require_once __DIR__ .
'/composer'
.
'/autoload_real.php'
;
return
ComposerAutoloaderInitDrupal8::getLoader();
|
composer这个工具大概的目的就是根据类名的及类的命名空间找到类文件所在位置,然后使用该类的时候自动加载这个类文件。相当于以前的require,只是变成自动的,当然它有自己的命名规范
想要了解的可以查看资料,之前找了一份,觉着挺好的,是个台湾人写的