contributte / apitte

:wrench: An opinionated and enjoyable API framework based on Nette Framework. Supporting content negotiation, debugging, middlewares, attributes, annotations and loving openapi/swagger.

Home Page:https://contributte.org/packages/contributte/apitte/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Where to close database connection?

vylink opened this issue · comments

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

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

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
  }

}

@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