Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
1 contributor

Users who have contributed to this file

29 lines (21 sloc) 764 Bytes

Service components

Application may get complicated so it makes sense to extract focused parts of business logic or infrastructure into service components. These components are typically instantiated once and put into dependency injection container.

These components are typically accessed either from other components or from controller. It is typically done via autowiring:

public function actionIndex(ServerRequestInterface $request, MyService $myService): ResponseInterface
{
    $id = $request->getAttribute('id');
    
    // ...
    $extraData = $myService->getExtraData($id);
    
    // ...
}

Note that in many cases it makes sense to choose more specific class to place your code into. Check:

  • Repository
  • Widget
  • Middleware
  • Entity
You can’t perform that action at this time.