Skip to content

Commit

Permalink
Add custom controller documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
mevdschee committed Oct 26, 2021
1 parent a639b68 commit 3293daa
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 38 deletions.
69 changes: 37 additions & 32 deletions README.md
Expand Up @@ -1316,6 +1316,43 @@ And this should return status 200 and as data:

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

## Custom Endpoints with Controller

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
custom endpoints to the existing router and with a callback that implements your own logic.

Here is an example of a custom controller class:

```
class MyHelloController {
private $responder;
public function __construct(Router $router, Responder $responder, GenericDB $db, ReflectionService $reflection, Cache $cache)
{
$router->register('GET', '/hello', array($this, 'getHello'));
$this->responder = $responder;
}
public function getHello(ServerRequestInterface $request): ResponseInterface
{
return $this->responder->success(['message' => "Hello World!"]);
}
}
```

And then you may register your custom controller class in the config object like this:

```
$config = new Config([
'customControllers' => 'MyHelloController',
...
]);
```

The `customControllers` config supports a comma separated list of custom controllers classes.

## Tests

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

Enjoy!

## Custom Endpoints with Controller

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
custom endpoints to the existing router and with a callback that implements your own logic.

Here is an example of custom controller class:

```
class MyHelloController {
public function __construct(Router $router, Responder $responder, RecordService $service)
{
$router->register('GET', '/hello', array($this, '_getHello'));
}
function _getHello(ServerRequestInterface $request): ResponseInterface
{
return $this->responder->success(['message' => "Hello World!"]);
}
}
```

And then you may register your custom controller class in the config object like this:

```
$config = new Config([
'customControllers' => 'MyHelloController',
...
]);
```

The `customControllers` config supports a comma separated list of custom controllers classes.
3 changes: 1 addition & 2 deletions api.include.php
Expand Up @@ -11431,8 +11431,7 @@ public function __construct(Config $config)
}
foreach ($config->getCustomControllers() as $className) {
if (class_exists($className)) {
$records = new RecordService($db, $reflection);
new $className($router, $responder, $records);
new $className($router, $responder, $db, $reflection, $cache);
}
}
$this->router = $router;
Expand Down
3 changes: 1 addition & 2 deletions api.php
Expand Up @@ -11431,8 +11431,7 @@ public function __construct(Config $config)
}
foreach ($config->getCustomControllers() as $className) {
if (class_exists($className)) {
$records = new RecordService($db, $reflection);
new $className($router, $responder, $records);
new $className($router, $responder, $db, $reflection, $cache);
}
}
$this->router = $router;
Expand Down
3 changes: 1 addition & 2 deletions src/Tqdev/PhpCrudApi/Api.php
Expand Up @@ -158,8 +158,7 @@ public function __construct(Config $config)
}
foreach ($config->getCustomControllers() as $className) {
if (class_exists($className)) {
$records = new RecordService($db, $reflection);
new $className($router, $responder, $records);
new $className($router, $responder, $db, $reflection, $cache);
}
}
$this->router = $router;
Expand Down
4 changes: 4 additions & 0 deletions tests/config/base.php
@@ -1,4 +1,7 @@
<?php

include_once 'controller.php';

$settings = [
'database' => 'incorrect_database',
'username' => 'incorrect_username',
Expand Down Expand Up @@ -64,5 +67,6 @@
'json.controllers' => 'records',
'json.tables' => 'products',
'json.columns' => 'properties',
'customControllers' => 'MyHelloController',
'debug' => false,
];
25 changes: 25 additions & 0 deletions tests/config/controller.php
@@ -0,0 +1,25 @@
<?php

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Tqdev\PhpCrudApi\Cache\Cache;
use Tqdev\PhpCrudApi\Column\ReflectionService;
use Tqdev\PhpCrudApi\Controller\Responder;
use Tqdev\PhpCrudApi\Database\GenericDB;
use Tqdev\PhpCrudApi\Middleware\Router\Router;

class MyHelloController {

private $responder;

public function __construct(Router $router, Responder $responder, GenericDB $db, ReflectionService $reflection, Cache $cache)
{
$router->register('GET', '/hello', array($this, 'getHello'));
$this->responder = $responder;
}

public function getHello(ServerRequestInterface $request): ResponseInterface
{
return $this->responder->success(['message' => "Hello World!"]);
}
}
8 changes: 8 additions & 0 deletions tests/functional/005_custom_controller/001_hello_world.log
@@ -0,0 +1,8 @@
===
GET /hello
===
200
Content-Type: application/json; charset=utf-8
Content-Length: 26

{"message":"Hello World!"}

0 comments on commit 3293daa

Please sign in to comment.