2
2
3
3
Single file PHP script that adds a REST API to a MySQL/MariaDB, PostgreSQL, SQL Server or SQLite database.
4
4
5
- NB: This is the [ TreeQL] ( https://treeql.org ) reference implementation in PHP.
6
-
7
- Related projects:
8
-
9
- - [ JS-CRUD-API] ( https://github.com/thipages/js-crud-api ) : A JavaScript client library for the API of PHP-CRUD-API
10
- - [ PHP-API-AUTH] ( https://github.com/mevdschee/php-api-auth ) : Single file PHP script that is an authentication provider for PHP-CRUD-API
11
- - [ PHP-CRUD-UI] ( https://github.com/mevdschee/php-crud-ui ) : Single file PHP script that adds a UI to a PHP-CRUD-API project.
12
- - [ PHP-CRUD-ADMIN] ( https://github.com/mevdschee/php-crud-admin ) : Single file PHP script that adds a database admin interface to a PHP-CRUD-API project.
13
- - [ PHP-SP-API] ( https://github.com/mevdschee/php-sp-api ) : Single file PHP script that adds a REST API to a SQL database.
14
- - [ VUE-CRUD-UI] ( https://github.com/nlware/vue-crud-ui ) : Single file Vue.js script that adds a UI to a PHP-CRUD-API project.
15
-
16
- There are also ports of this script in:
17
-
18
- - [ Java JDBC by Ivan Kolchagov] ( https://github.com/kolchagov/java-crud-api ) (v1)
19
- - [ Java Spring Boot + jOOQ] ( https://github.com/mevdschee/java-crud-api/tree/master/full ) (v2: work in progress)
5
+ Howto: Upload "` api.php ` " to your webserver, configure it to connect to your database, have an instant full-featured REST API.
20
6
21
- There are also proof-of-concept ports of this script that only support basic REST CRUD functionality in:
22
- [ PHP] ( https://github.com/mevdschee/php-crud-api/blob/master/extras/core.php ) ,
23
- [ Java] ( https://github.com/mevdschee/java-crud-api/blob/master/core/src/main/java/com/tqdev/CrudApiHandler.java ) ,
24
- [ Go] ( https://github.com/mevdschee/go-crud-api/blob/master/api.go ) ,
25
- [ C# .net core] ( https://github.com/mevdschee/core-data-api/blob/master/Program.cs ) ,
26
- [ Node.js] ( https://github.com/mevdschee/js-crud-api/blob/master/app.js ) and
27
- [ Python] ( https://github.com/mevdschee/py-crud-api/blob/master/api.py ) .
7
+ NB: This is the [ TreeQL] ( https://treeql.org ) reference implementation in PHP.
28
8
29
9
## Requirements
30
10
@@ -80,6 +60,7 @@ These are all the configuration options and their default value between brackets
80
60
- "tables": Comma separated list of tables to publish (defaults to 'all')
81
61
- "middlewares": List of middlewares to load (` cors ` )
82
62
- "controllers": List of controllers to load (` records,geojson,openapi,status ` )
63
+ - "customControllers": List of user custom controllers to load (no default)
83
64
- "openApiBase": OpenAPI info (` {"info":{"title":"PHP-CRUD-API","version":"1.0.0"}} ` )
84
65
- "cacheType": ` TempFile ` , ` Redis ` , ` Memcache ` , ` Memcached ` or ` NoCache ` (` TempFile ` )
85
66
- "cachePath": Path/address of the cache (defaults to system's temp directory)
@@ -139,6 +120,31 @@ The following features are supported:
139
120
- Security enhancing middleware is included
140
121
- Standard compliant: PSR-4, PSR-7, PSR-12, PSR-15 and PSR-17
141
122
123
+ ## Related projects and ports
124
+
125
+ Related projects:
126
+
127
+ - [ JS-CRUD-API] ( https://github.com/thipages/js-crud-api ) : A JavaScript client library for the API of PHP-CRUD-API
128
+ - [ PHP-API-AUTH] ( https://github.com/mevdschee/php-api-auth ) : Single file PHP script that is an authentication provider for PHP-CRUD-API
129
+ - [ PHP-CRUD-UI] ( https://github.com/mevdschee/php-crud-ui ) : Single file PHP script that adds a UI to a PHP-CRUD-API project.
130
+ - [ PHP-CRUD-ADMIN] ( https://github.com/mevdschee/php-crud-admin ) : Single file PHP script that adds a database admin interface to a PHP-CRUD-API project.
131
+ - [ PHP-SP-API] ( https://github.com/mevdschee/php-sp-api ) : Single file PHP script that adds a REST API to a SQL database.
132
+ - [ VUE-CRUD-UI] ( https://github.com/nlware/vue-crud-ui ) : Single file Vue.js script that adds a UI to a PHP-CRUD-API project.
133
+
134
+ There are also ports of this script in:
135
+
136
+ - [ Go-CRUD-API] ( https://github.com/dranih/go-crud-api ) (work in progress)
137
+ - [ Java JDBC by Ivan Kolchagov] ( https://github.com/kolchagov/java-crud-api ) (v1)
138
+ - [ Java Spring Boot + jOOQ] ( https://github.com/mevdschee/java-crud-api/tree/master/full ) (v2: work in progress)
139
+
140
+ There are also proof-of-concept ports of this script that only support basic REST CRUD functionality in:
141
+ [ PHP] ( https://github.com/mevdschee/php-crud-api/blob/master/extras/core.php ) ,
142
+ [ Java] ( https://github.com/mevdschee/java-crud-api/blob/master/core/src/main/java/com/tqdev/CrudApiHandler.java ) ,
143
+ [ Go] ( https://github.com/mevdschee/go-crud-api/blob/master/api.go ) ,
144
+ [ C# .net core] ( https://github.com/mevdschee/core-data-api/blob/master/Program.cs ) ,
145
+ [ Node.js] ( https://github.com/mevdschee/js-crud-api/blob/master/app.js ) and
146
+ [ Python] ( https://github.com/mevdschee/py-crud-api/blob/master/api.py ) .
147
+
142
148
## Compilation
143
149
144
150
You can install all dependencies of this project using the following command:
@@ -673,6 +679,9 @@ You can tune the middleware behavior using middleware specific configuration par
673
679
- "dbAuth.usernameColumn": The users table column that holds usernames ("username")
674
680
- "dbAuth.passwordColumn": The users table column that holds passwords ("password")
675
681
- "dbAuth.returnedColumns": The columns returned on successful login, empty means 'all' ("")
682
+ - "dbAuth.usernameFormField": The name of the form field that holds the username ("username")
683
+ - "dbAuth.passwordFormField": The name of the form field that holds the password ("password")
684
+ - "dbAuth.newPasswordFormField": The name of the form field that holds the new password ("newPassword")
676
685
- "dbAuth.registerUser": JSON user data (or "1") in case you want the /register endpoint enabled ("")
677
686
- "dbAuth.passwordLength": Minimum length that the password must have ("12")
678
687
- "dbAuth.sessionName": The name of the PHP session that is started ("")
@@ -1315,6 +1324,53 @@ And this should return status 200 and as data:
1315
1324
1316
1325
These can be used to measure the time (in microseconds) to connect and read data from the database and the cache.
1317
1326
1327
+ ## Custom controller
1328
+
1329
+ You can add your own custom REST API endpoints by writing your own custom controller class.
1330
+ The class must provide a constructor that accepts five parameters. With these parameters you can register
1331
+ your own endpoint to the existing router. This endpoint may use the database and/or the reflection class
1332
+ of the database.
1333
+
1334
+ Here is an example of a custom controller class:
1335
+
1336
+ ```
1337
+ use Psr\Http\Message\ResponseInterface;
1338
+ use Psr\Http\Message\ServerRequestInterface;
1339
+ use Tqdev\PhpCrudApi\Cache\Cache;
1340
+ use Tqdev\PhpCrudApi\Column\ReflectionService;
1341
+ use Tqdev\PhpCrudApi\Controller\Responder;
1342
+ use Tqdev\PhpCrudApi\Database\GenericDB;
1343
+ use Tqdev\PhpCrudApi\Middleware\Router\Router;
1344
+
1345
+ class MyHelloController {
1346
+
1347
+ private $responder;
1348
+
1349
+ public function __construct(Router $router, Responder $responder, GenericDB $db, ReflectionService $reflection, Cache $cache)
1350
+ {
1351
+ $router->register('GET', '/hello', array($this, 'getHello'));
1352
+ $this->responder = $responder;
1353
+ }
1354
+
1355
+ public function getHello(ServerRequestInterface $request): ResponseInterface
1356
+ {
1357
+ return $this->responder->success(['message' => "Hello World!"]);
1358
+ }
1359
+ }
1360
+ ```
1361
+
1362
+ And then you may register your custom controller class in the config object like this:
1363
+
1364
+ ```
1365
+ $config = new Config([
1366
+ ...
1367
+ 'customControllers' => 'MyHelloController',
1368
+ ...
1369
+ ]);
1370
+ ```
1371
+
1372
+ The ` customControllers ` config supports a comma separated list of custom controller classes.
1373
+
1318
1374
## Tests
1319
1375
1320
1376
I am testing mainly on Ubuntu and I have the following test setups:
@@ -1324,15 +1380,17 @@ I am testing mainly on Ubuntu and I have the following test setups:
1324
1380
- (Docker) Ubuntu 18.04 with PHP 7.2, MySQL 5.7, PostgreSQL 10.4 (PostGIS 2.4) and SQLite 3.22
1325
1381
- (Docker) Debian 10 with PHP 7.3, MariaDB 10.3, PostgreSQL 11.4 (PostGIS 2.5) and SQLite 3.27
1326
1382
- (Docker) Ubuntu 20.04 with PHP 7.4, MySQL 8.0, PostgreSQL 12.2 (PostGIS 3.0) and SQLite 3.31
1327
- - (Docker) CentOS 8 with PHP 8.0, MariaDB 10.5, PostgreSQL 12.5 (PostGIS 3.0) and SQLite 3.26
1383
+ - (Docker) CentOS 8 with PHP 8.1, MariaDB 10.6, PostgreSQL 12.8 (PostGIS 3.0) and SQLite 3.26
1384
+ - (Docker) Debian 11 with PHP 7.4, MariaDB 10.5, PostgreSQL 13.4 (PostGIS 3.1) and SQLite 3.34
1328
1385
1329
1386
This covers not all environments (yet), so please notify me of failing tests and report your environment.
1330
1387
I will try to cover most relevant setups in the "docker" folder of the project.
1331
1388
1332
1389
### Running
1333
1390
1334
- To run the functional tests locally you may run the following command :
1391
+ To run the functional tests locally you may run the following commands :
1335
1392
1393
+ php build.php
1336
1394
php test.php
1337
1395
1338
1396
This runs the functional tests from the "tests" directory. It uses the database dumps (fixtures) and
@@ -1379,17 +1437,17 @@ Install docker using the following commands and then logout and login for the ch
1379
1437
To run the docker tests run "build_all.sh" and "run_all.sh" from the docker directory. The output should be:
1380
1438
1381
1439
================================================
1382
- CentOS 8 (PHP 8.0 )
1440
+ CentOS 8 (PHP 8.1 )
1383
1441
================================================
1384
- [1/4] Starting MariaDB 10.5 ..... done
1385
- [2/4] Starting PostgreSQL 12.5 .. done
1442
+ [1/4] Starting MariaDB 10.6 ..... done
1443
+ [2/4] Starting PostgreSQL 12.8 .. done
1386
1444
[3/4] Starting SQLServer 2017 ... skipped
1387
1445
[4/4] Cloning PHP-CRUD-API v2 ... skipped
1388
1446
------------------------------------------------
1389
- mysql: 110 tests ran in 957 ms, 1 skipped, 0 failed
1390
- pgsql: 110 tests ran in 817 ms, 1 skipped, 0 failed
1447
+ mysql: 117 tests ran in 1336 ms, 1 skipped, 0 failed
1448
+ pgsql: 117 tests ran in 1316 ms, 1 skipped, 0 failed
1391
1449
sqlsrv: skipped, driver not loaded
1392
- sqlite: 110 tests ran in 685 ms, 12 skipped, 0 failed
1450
+ sqlite: 117 tests ran in 958 ms, 13 skipped, 0 failed
1393
1451
================================================
1394
1452
Debian 10 (PHP 7.3)
1395
1453
================================================
@@ -1398,10 +1456,22 @@ To run the docker tests run "build_all.sh" and "run_all.sh" from the docker dire
1398
1456
[3/4] Starting SQLServer 2017 ... skipped
1399
1457
[4/4] Cloning PHP-CRUD-API v2 ... skipped
1400
1458
------------------------------------------------
1401
- mysql: 110 tests ran in 952 ms, 1 skipped, 0 failed
1402
- pgsql: 110 tests ran in 816 ms, 1 skipped, 0 failed
1459
+ mysql: 117 tests ran in 1276 ms, 1 skipped, 0 failed
1460
+ pgsql: 117 tests ran in 1364 ms, 1 skipped, 0 failed
1403
1461
sqlsrv: skipped, driver not loaded
1404
- sqlite: 110 tests ran in 690 ms, 12 skipped, 0 failed
1462
+ sqlite: 117 tests ran in 948 ms, 13 skipped, 0 failed
1463
+ ================================================
1464
+ Debian 11 (PHP 7.4)
1465
+ ================================================
1466
+ [1/4] Starting MariaDB 10.5 ..... done
1467
+ [2/4] Starting PostgreSQL 13.4 .. done
1468
+ [3/4] Starting SQLServer 2017 ... skipped
1469
+ [4/4] Cloning PHP-CRUD-API v2 ... skipped
1470
+ ------------------------------------------------
1471
+ mysql: 117 tests ran in 1255 ms, 1 skipped, 0 failed
1472
+ pgsql: 117 tests ran in 1329 ms, 1 skipped, 0 failed
1473
+ sqlsrv: skipped, driver not loaded
1474
+ sqlite: 117 tests ran in 972 ms, 13 skipped, 0 failed
1405
1475
================================================
1406
1476
Debian 9 (PHP 7.0)
1407
1477
================================================
@@ -1410,10 +1480,10 @@ To run the docker tests run "build_all.sh" and "run_all.sh" from the docker dire
1410
1480
[3/4] Starting SQLServer 2017 ... skipped
1411
1481
[4/4] Cloning PHP-CRUD-API v2 ... skipped
1412
1482
------------------------------------------------
1413
- mysql: 110 tests ran in 1075 ms, 1 skipped, 0 failed
1414
- pgsql: 110 tests ran in 834 ms, 1 skipped, 0 failed
1483
+ mysql: 117 tests ran in 1475 ms, 1 skipped, 0 failed
1484
+ pgsql: 117 tests ran in 1394 ms, 1 skipped, 0 failed
1415
1485
sqlsrv: skipped, driver not loaded
1416
- sqlite: 110 tests ran in 728 ms, 12 skipped, 0 failed
1486
+ sqlite: 117 tests ran in 1065 ms, 13 skipped, 0 failed
1417
1487
================================================
1418
1488
Ubuntu 16.04 (PHP 7.0)
1419
1489
================================================
@@ -1422,9 +1492,9 @@ To run the docker tests run "build_all.sh" and "run_all.sh" from the docker dire
1422
1492
[3/4] Starting SQLServer 2017 ... done
1423
1493
[4/4] Cloning PHP-CRUD-API v2 ... skipped
1424
1494
------------------------------------------------
1425
- mysql: 110 tests ran in 1065 ms, 1 skipped, 0 failed
1426
- pgsql: 110 tests ran in 845 ms, 1 skipped, 0 failed
1427
- sqlsrv: 110 tests ran in 5404 ms, 1 skipped, 0 failed
1495
+ mysql: 117 tests ran in 1434 ms, 1 skipped, 0 failed
1496
+ pgsql: 117 tests ran in 1432 ms, 1 skipped, 0 failed
1497
+ sqlsrv: 117 tests ran in 8634 ms, 1 skipped, 0 failed
1428
1498
sqlite: skipped, driver not loaded
1429
1499
================================================
1430
1500
Ubuntu 18.04 (PHP 7.2)
@@ -1434,33 +1504,34 @@ To run the docker tests run "build_all.sh" and "run_all.sh" from the docker dire
1434
1504
[3/4] Starting SQLServer 2017 ... skipped
1435
1505
[4/4] Cloning PHP-CRUD-API v2 ... skipped
1436
1506
------------------------------------------------
1437
- mysql: 110 tests ran in 1261 ms, 1 skipped, 0 failed
1438
- pgsql: 110 tests ran in 859 ms, 1 skipped, 0 failed
1507
+ mysql: 117 tests ran in 1687 ms, 1 skipped, 0 failed
1508
+ pgsql: 117 tests ran in 1393 ms, 1 skipped, 0 failed
1439
1509
sqlsrv: skipped, driver not loaded
1440
- sqlite: 110 tests ran in 725 ms, 12 skipped, 0 failed
1510
+ sqlite: 117 tests ran in 1158 ms, 13 skipped, 0 failed
1441
1511
================================================
1442
1512
Ubuntu 20.04 (PHP 7.4)
1443
1513
================================================
1444
1514
[1/4] Starting MySQL 8.0 ........ done
1445
1515
[2/4] Starting PostgreSQL 12.2 .. done
1446
- [3/4] Starting SQLServer 2017 ... skipped
1516
+ [3/4] Starting SQLServer 2019 ... done
1447
1517
[4/4] Cloning PHP-CRUD-API v2 ... skipped
1448
1518
------------------------------------------------
1449
- mysql: 110 tests ran in 1505 ms, 1 skipped, 0 failed
1450
- pgsql: 110 tests ran in 851 ms, 1 skipped, 0 failed
1451
- sqlsrv: skipped, driver not loaded
1452
- sqlite: 110 tests ran in 675 ms, 12 skipped, 0 failed
1519
+ mysql: 117 tests ran in 2096 ms, 1 skipped, 0 failed
1520
+ pgsql: 117 tests ran in 1368 ms, 1 skipped, 0 failed
1521
+ sqlsrv: 117 tests ran in 8410 ms, 1 skipped, 0 failed
1522
+ sqlite: 117 tests ran in 1053 ms, 13 skipped, 0 failed
1453
1523
1454
1524
The above test run (including starting up the databases) takes less than 5 minutes on my slow laptop.
1455
1525
1456
1526
$ ./run.sh
1457
1527
1) centos8
1458
1528
2) debian10
1459
- 3) debian9
1460
- 4) ubuntu16
1461
- 5) ubuntu18
1462
- 6) ubuntu20
1463
- > 5
1529
+ 3) debian11
1530
+ 4) debian9
1531
+ 5) ubuntu16
1532
+ 6) ubuntu18
1533
+ 7) ubuntu20
1534
+ > 6
1464
1535
================================================
1465
1536
Ubuntu 18.04 (PHP 7.2)
1466
1537
================================================
@@ -1469,13 +1540,13 @@ The above test run (including starting up the databases) takes less than 5 minut
1469
1540
[3/4] Starting SQLServer 2017 ... skipped
1470
1541
[4/4] Cloning PHP-CRUD-API v2 ... skipped
1471
1542
------------------------------------------------
1472
- mysql: 110 tests ran in 1261 ms, 1 skipped, 0 failed
1473
- pgsql: 110 tests ran in 859 ms, 1 skipped, 0 failed
1543
+ mysql: 117 tests ran in 1687 ms, 1 skipped, 0 failed
1544
+ pgsql: 117 tests ran in 1393 ms, 1 skipped, 0 failed
1474
1545
sqlsrv: skipped, driver not loaded
1475
- sqlite: 110 tests ran in 725 ms, 12 skipped, 0 failed
1546
+ sqlite: 117 tests ran in 1158 ms, 13 skipped, 0 failed
1476
1547
root@b7ab9472e08f:/php-crud-api#
1477
1548
1478
- As you can see the "run.sh" script gives you access to a prompt in a chosen the docker environment.
1549
+ As you can see the "run.sh" script gives you access to a prompt in the chosen docker environment.
1479
1550
In this environment the local files are mounted. This allows for easy debugging on different environments.
1480
1551
You may type "exit" when you are done.
1481
1552
0 commit comments