What is the “proper” Slim way of communicating between the router and other portions of the application? I’ve included two examples below. Thanks
Option 1
$api->post('/users', function(Request $request, Response $response) {
try {
$newUser = $this->apiUserService->create($request->getParsedBody());
return $response->write($newUser);
} catch (PermissionDeniedError $e) {
return $response->write($e->getMessage())->withStatus(401);
} catch (UserValidationError $e) {
return $response->write($e->getMessage())->withStatus(422);
}
});
class apiUserService
{
public function create(array $params): User
{
// If successful, return user else throw exception
}
}
Option 2
$api->post('/users', function(Request $request, Response $response) {
return $this->apiUserService->create($request->getParsedBody(), $response);
});
class apiUserService
{
public function create(array $params, Response $response): Response
{
// Modify $response body and status as applicable and return $response for all conditions
}
}