CakePHP: where to put the logic of "Services"

I come from the Java / Grails background and cannot find a definitive answer on the Internet about where the service logic for the CakePHP application should be stored. “Services,” I'm talking about classes that are usually created through dependency injection to conduct business logic on domain objects. They should be able to query any domain object and make changes in response to the action of the controller.

CakePHP's "Component" class currently seems to be the closest to this behavior. I can load the component into any controller and execute its methods as needed. However, I read in several places that components should never access the database, and that this will lead to some cool performance results.

I also looked at CakePHP's Behavior class, and it doesn't seem to fit the ticket at all. It seems well-equipped for organizing domain objects in setting up the data structure, but this is not the logic that the service will execute. Also, in order to import any model definition into Behavior, I would have to edit the model definition itself to allow access, which is very inconvenient.

So, I ask this question: where should the service logic be stored? Of course, not a controller, since it should contain only minimal logic to process the request and send a response.

+6
source share
2 answers

Components is a service level in CakePHP. They are created by a collection collection container for dependencies and passed to the controller, requests and responses to be processed.

There is no limit to what Components can do except to maintain separation between layers. It’s good to use database connections or use models directly from the component and modify the query.

The components are actually very lightweight if you only make them for specific cases. Validating an action name is a general way to limit the scope of a component. You can also enter a setting so that it can know when it is normal to execute custom service logic.

+6
source

So, I ask this question: where should the service logic be stored? Of course, not a controller, since it should contain only minimal logic to process the request and send a response.

Sounds like the perfect use for Dispatcher Filter . It is called before the controller instance is created. If you need to query the database, simply load the model through ClassRegistry :: init ('YourModelName') and pass the query parameters to the model method and return everything you need in your query. No controller is needed at all. We implemented oauth + xhttp using Dispatcher Filters without calling the controller.

How using a model inside a component should affect performance ... I don’t know who got this strange idea, this is not the best article you found. It is true that you should not put the model modeling logic in them, but you can call the model, for example, through an instance of the controller and controller models.

-2
source

Source: https://habr.com/ru/post/950364/


All Articles