Skip to content

Commit a639b68

Browse files
authored
docs: Add 'customControllers' config parameter to README #766 (#812)
* docs: Add 'customControllers' config parameter to README #766 * docs: Add 'customControllers' config parameter to README #766 * docs: Add 'customControllers' config parameter to README #766
1 parent 806c136 commit a639b68

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

README.md

+33
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ These are all the configuration options and their default value between brackets
8080
- "tables": Comma separated list of tables to publish (defaults to 'all')
8181
- "middlewares": List of middlewares to load (`cors`)
8282
- "controllers": List of controllers to load (`records,geojson,openapi,status`)
83+
- "customControllers": List of user custom controllers to load (no default)
8384
- "openApiBase": OpenAPI info (`{"info":{"title":"PHP-CRUD-API","version":"1.0.0"}}`)
8485
- "cacheType": `TempFile`, `Redis`, `Memcache`, `Memcached` or `NoCache` (`TempFile`)
8586
- "cachePath": Path/address of the cache (defaults to system's temp directory)
@@ -1504,3 +1505,35 @@ Test the script (running in the container) by opening the following URL:
15041505
http://localhost:8080/records/posts/1
15051506

15061507
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.

0 commit comments

Comments
 (0)