Skip to content

Commit 3293daa

Browse files
committed
Add custom controller documentation
1 parent a639b68 commit 3293daa

File tree

7 files changed

+77
-38
lines changed

7 files changed

+77
-38
lines changed

README.md

+37-32
Original file line numberDiff line numberDiff line change
@@ -1316,6 +1316,43 @@ And this should return status 200 and as data:
13161316

13171317
These can be used to measure the time (in microseconds) to connect and read data from the database and the cache.
13181318

1319+
## Custom Endpoints with Controller
1320+
1321+
You can add your own custom REST API endpoints by writing your own custom controller class.
1322+
The class must provide a constructor that accepts three parameters. These parameters will allow you to register
1323+
custom endpoints to the existing router and with a callback that implements your own logic.
1324+
1325+
Here is an example of a custom controller class:
1326+
1327+
```
1328+
class MyHelloController {
1329+
1330+
private $responder;
1331+
1332+
public function __construct(Router $router, Responder $responder, GenericDB $db, ReflectionService $reflection, Cache $cache)
1333+
{
1334+
$router->register('GET', '/hello', array($this, 'getHello'));
1335+
$this->responder = $responder;
1336+
}
1337+
1338+
public function getHello(ServerRequestInterface $request): ResponseInterface
1339+
{
1340+
return $this->responder->success(['message' => "Hello World!"]);
1341+
}
1342+
}
1343+
```
1344+
1345+
And then you may register your custom controller class in the config object like this:
1346+
1347+
```
1348+
$config = new Config([
1349+
'customControllers' => 'MyHelloController',
1350+
...
1351+
]);
1352+
```
1353+
1354+
The `customControllers` config supports a comma separated list of custom controllers classes.
1355+
13191356
## Tests
13201357

13211358
I am testing mainly on Ubuntu and I have the following test setups:
@@ -1505,35 +1542,3 @@ Test the script (running in the container) by opening the following URL:
15051542
http://localhost:8080/records/posts/1
15061543

15071544
Enjoy!
1508-
1509-
## Custom Endpoints with Controller
1510-
1511-
You can add your own custom REST API endpoints by writing your own custom controller class. The class must provide a constructor that accepts three parameters. These parameters will allow you to register
1512-
custom endpoints to the existing router and with a callback that implements your own logic.
1513-
1514-
Here is an example of custom controller class:
1515-
1516-
```
1517-
class MyHelloController {
1518-
public function __construct(Router $router, Responder $responder, RecordService $service)
1519-
{
1520-
$router->register('GET', '/hello', array($this, '_getHello'));
1521-
}
1522-
1523-
function _getHello(ServerRequestInterface $request): ResponseInterface
1524-
{
1525-
return $this->responder->success(['message' => "Hello World!"]);
1526-
}
1527-
}
1528-
```
1529-
1530-
And then you may register your custom controller class in the config object like this:
1531-
1532-
```
1533-
$config = new Config([
1534-
'customControllers' => 'MyHelloController',
1535-
...
1536-
]);
1537-
```
1538-
1539-
The `customControllers` config supports a comma separated list of custom controllers classes.

api.include.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -11431,8 +11431,7 @@ public function __construct(Config $config)
1143111431
}
1143211432
foreach ($config->getCustomControllers() as $className) {
1143311433
if (class_exists($className)) {
11434-
$records = new RecordService($db, $reflection);
11435-
new $className($router, $responder, $records);
11434+
new $className($router, $responder, $db, $reflection, $cache);
1143611435
}
1143711436
}
1143811437
$this->router = $router;

api.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -11431,8 +11431,7 @@ public function __construct(Config $config)
1143111431
}
1143211432
foreach ($config->getCustomControllers() as $className) {
1143311433
if (class_exists($className)) {
11434-
$records = new RecordService($db, $reflection);
11435-
new $className($router, $responder, $records);
11434+
new $className($router, $responder, $db, $reflection, $cache);
1143611435
}
1143711436
}
1143811437
$this->router = $router;

src/Tqdev/PhpCrudApi/Api.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,7 @@ public function __construct(Config $config)
158158
}
159159
foreach ($config->getCustomControllers() as $className) {
160160
if (class_exists($className)) {
161-
$records = new RecordService($db, $reflection);
162-
new $className($router, $responder, $records);
161+
new $className($router, $responder, $db, $reflection, $cache);
163162
}
164163
}
165164
$this->router = $router;

tests/config/base.php

+4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
<?php
2+
3+
include_once 'controller.php';
4+
25
$settings = [
36
'database' => 'incorrect_database',
47
'username' => 'incorrect_username',
@@ -64,5 +67,6 @@
6467
'json.controllers' => 'records',
6568
'json.tables' => 'products',
6669
'json.columns' => 'properties',
70+
'customControllers' => 'MyHelloController',
6771
'debug' => false,
6872
];

tests/config/controller.php

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
use Psr\Http\Message\ResponseInterface;
4+
use Psr\Http\Message\ServerRequestInterface;
5+
use Tqdev\PhpCrudApi\Cache\Cache;
6+
use Tqdev\PhpCrudApi\Column\ReflectionService;
7+
use Tqdev\PhpCrudApi\Controller\Responder;
8+
use Tqdev\PhpCrudApi\Database\GenericDB;
9+
use Tqdev\PhpCrudApi\Middleware\Router\Router;
10+
11+
class MyHelloController {
12+
13+
private $responder;
14+
15+
public function __construct(Router $router, Responder $responder, GenericDB $db, ReflectionService $reflection, Cache $cache)
16+
{
17+
$router->register('GET', '/hello', array($this, 'getHello'));
18+
$this->responder = $responder;
19+
}
20+
21+
public function getHello(ServerRequestInterface $request): ResponseInterface
22+
{
23+
return $this->responder->success(['message' => "Hello World!"]);
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
===
2+
GET /hello
3+
===
4+
200
5+
Content-Type: application/json; charset=utf-8
6+
Content-Length: 26
7+
8+
{"message":"Hello World!"}

0 commit comments

Comments
 (0)