dhtmlxGantt 与 PHP:Slim
本教程将为您提供所有必要步骤,使用 Slim 4 框架和服务器端的 RESTful API 构建基于 PHP 的甘特图。
本教程使用 Slim Framework v4.x。如果您正在使用旧版本,请参考 Slim Framework v3.x 指南。
此外,我们还提供了与其他平台和框架集成的教程:
- dhtmlxGantt와 ASP.NET Core 사용하기
- dhtmlxGantt와 ASP.NET MVC
- dhtmlxGantt와 Node.js 연동하기
- dhtmlxGantt와 Python
- dhtmlxGantt와 PHP: Laravel 연동
- dhtmlxGantt와 Salesforce LWC 연동하기
- dhtmlxGantt와 Ruby on Rails 연동하기
在本指南中,将使用 Slim 4 框架进行路由,MySQL 作为数据存储。CRUD 操作通过 PDO 实现,并设计得足够灵活,可适配其他框架。
完整源代码可在 GitHub 获取。
步骤 1. 初始化项目
创建项目
我们将从使用为 Slim 4 提供的 skeleton application 开始。
首先,导入项目并使用 Composer 安装依赖:
php composer.phar create-project slim/slim-skeleton gantt-rest-php
如果您的系统已全局安装 Composer,可以运行:
composer create-project slim/slim-skeleton gantt-rest-php
接下来,进入项目文件夹并启动 web 服务器,验证设置是否正确:
cd gantt-rest-php
php -S 0.0.0.0:8080 -t public public/index.php
然后,在浏览器中打开 http://127.0.0.1:8080,即可看到 Slim 的默认欢迎页面。
步 骤 2. 在页面中添加 Gantt
下一步是创建一个显示甘特图的页面。这将分为两个简单的步骤完成。
创建视图
首先,在 app/templates 文件夹中创建一个名为 basic.html 的文件。该文件将用于显示甘特图,并包含加载数据所需的基本设置。
完整代码如下:
app/templates/basic.html
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charSet="utf-8"">
<script src="https://cdn.dhtmlx.com/gantt/edge/dhtmlxgantt.js"></script>
<link href="https://cdn.dhtmlx.com/gantt/edge/dhtmlxgantt.css" rel="stylesheet">
<style type="text/css">
html, body{
height:100%;
padding:0px;
margin:0px;
overflow: hidden;
}
</style>
</head>
<body>
<div id="gantt_here" style='width:100%; height:100%;'></div>
<script type="text/javascript">
gantt.init("gantt_here");
</script>
</body>
</html>
这样便在页面上创建了一个空的甘特图。用户可以创建和修改任务及链接,但刷新页面后更改不会被保存。
设置路由
新页面准备好后,需要通过浏览器访问。请在 app/routes.php 中添加如下路由:
app/routes.php
$app->get('/', function (Request $request, Response $response) {
$payload = file_get_contents(__DIR__.'/templates/basic.html');
$response->getBody()->write($payload);
return $response;
});
使用以下命令重启应用程序:
command line
php -S 0.0.0.0:8080 -t public public/index.php
现在,在浏览器访问 http://127.0.0.1:8080/,即可在页面上显示甘特图。

步骤 3. 配置数据库
甘特图页面显示后,下一步是创建数据库并将其连接到应用程序。
创建数据库
可以使用任何常用的 MySQL 客户端(如 phpMyAdmin)或直接通过命令行来创建数据库。下面提供了一个 SQL 脚本,用于创建包含两个数据表的简单数据库。
CREATE DATABASE IF NOT EXISTS `gantt`;
USE `gantt`;
CREATE TABLE `gantt_links` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`source` int(11) NOT NULL,
`target` int(11) NOT NULL,
`type` varchar(1) NOT NULL,
PRIMARY KEY (`id`)
);
CREATE TABLE `gantt_tasks` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`text` varchar(255) NOT NULL,
`start_date` datetime NOT NULL,
`duration` int(11) NOT NULL,
`progress` float NOT NULL,
`parent` int(11) NOT NULL,
PRIMARY KEY (`id`)
);
数据库创建完成后,可以向 gantt_tasks 表中插入一些示例数据用于测试。请使用如下 SQL 命令:
INSERT INTO `gantt_tasks` VALUES ('1', 'Project #1', '2020-03-31 00:00:00',
'4', '0.8', '0');
INSERT INTO `gantt_tasks` VALUES ('2', 'Task #1', '2020-03-31 00:00:00',
'3', '0.5', '1');
INSERT INTO `gantt_tasks` VALUES ('3', 'Task #2', '2020-04-01 00:00:00',
'2', '0.7', '1');
INSERT INTO `gantt_tasks` VALUES ('4', 'Task #3', '2020-04-02 00:00:00',
'2', '0', '1');
INSERT INTO `gantt_tasks` VALUES ('5', 'Task #1.1', '2020-04-03 00:00:00',
'3', '0.34', '2');
INSERT INTO `gantt_tasks` VALUES ('6', 'Task #1.2', '2020-04-03 13:22:17',
'2', '0.5', '2');
INSERT INTO `gantt_tasks` VALUES ('7', 'Task #2.1', '2020-04-04 00:00:00',
'3', '0.2', '3');
INSERT INTO `gantt_tasks` VALUES ('8', 'Task #2.2', '2020-04-05 00:00:00',
'2', '0.9', '3');
如需更详细的示例,请参阅此处。
项目配置完成后,下一步是加载数据。