contributte / apitte

:wrench: An opinionated and enjoyable API framework based on Nette Framework. Supporting content negotiation, debugging, middlewares, attributes, annotations and loving openapi/swagger.
https://contributte.org/packages/contributte/apitte/
MIT License
61 stars 37 forks source link

Where to close database connection? #106

Closed vylink closed 5 years ago

vylink commented 5 years ago

I would like to close database connection after each request. What will be the best approach? Where I can do that? Any idea?

mabar commented 5 years ago

Probably response and exception decorators. We will maybe add some events to Application class, but not now.

mabar commented 5 years ago

Related to #105

use Apitte\Core\UI\Controller\IController;
use Apitte\Core\UI\Controller\Lifecycle\AfterAction;
use Apitte\Core\UI\Controller\Lifecycle\BeforeAction;

class ExampleController implements IController, BeforeAction, AfterAction
{

  public function before(ApiRequest $request): ApiRequest
  {
    // some initialization
  }

  public function endpoint(ApiRequest $request, ApiResponse $response): ApiResponse
  {
    return $response
      ->withStatus(ApiResponse::S200_OK);
  }

  public function after(ApiRequest $request, ApiResponse $response): ApiResponse
  {
    // cleanup, close database connection
  }

}
mabar commented 5 years ago

@vylink I was thinking about that for a while. Do you have an use-case where you really need endpoint-specific before and after methods? Because I think this is what middlewares should do