9

I come from a Java/Grails background and cannot seem to find a definite answer online as to where the service logic for a CakePHP application should be stored. By "Services", I am talking about classes that are usually instantiated 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 a controller action.

Currently, CakePHP's "Component" class seems to be the closest match to this behavior. I can load a component into any controller and execute its methods as needed. However, I have read in several places that components should never access the database and that doing so will result in some steep performance hits.

I have also looked into CakePHP's "Behavior" class and it doesn't seem to fit the ticket at all. It seems well-equipped to organize domain objects into a data structure setting, but that's not the kind of logic that a service would execute. Also, to import any model definition into a Behavior, I would have to edit the model definition itself to allow access, which is very awkward.

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

10
  • 3
    Here is a free hint for you: stay away from CakePHP. It's one of the worst frameworks in PHP and it definitely does not implement anything remotely like MVC. If want to use something that at least recognizes concept of "services", you might try Symfony2.
    – tereško
    Jul 26, 2013 at 14:34
  • Argument 1: global state. CakePHP is basically base around the use of singletons and other statically scoped parameters.
    – tereško
    Jul 26, 2013 at 14:50
  • Argument 2: static classes: most of CakePHP core consists of static methods. That makes all of the code tightly coupled to specific names of classes, which end up being used as namespace.
    – tereško
    Jul 26, 2013 at 14:51
  • 1
    @tereško I did not delete it guess it was some moderator. I agree with 1 and 2 and don't like it either. I disagree with 3, it might not be MVC but the way it works is just fine. I personally like the dumb templates. 4 is participially true, 5 is not true for 2.0. And I've tried myself to push a few changes to CakePHP but honestly getting things changed is a pain in the rear, more discussion than being productive at the end of the day. So I gave up and hope for 3.0.
    – floriank
    Jul 26, 2013 at 16:25
  • 1
    since someone else could come here and see these comments, I just wanted to give a heads up for newbies. I think all of the arguments here are resolved with CakePHP 3.X versions. Our team finds it very maintainable and easy to tweak with different kind of architectures when necessary. Apr 13, 2017 at 12:34

2 Answers 2

11

Components are the service layer in CakePHP. They are constructed by a dependency injection container (Components Collection) and get passed the controller, request and response that is to be handled.

There are no restrictions in what Components can do other than maintaining separation between layers. It is fine to use database connections, or use models, directly from a component and modify the request.

Components are actually very light-weighted if you only make them act for specific cases. Inspecting the action name, is a common way of limiting the reach of a component. You can also inject setting so it can know when is OK to execute custom service logic.

-2

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

Sounds like the ideal use case for a Dispatcher Filter. It gets called even before a controller is instantiated. If you need to query the database simply load a model via ClassRegistry::init('YourModelName') and pass the request params to the model method and return whatever you need in your request. No controller needed at all. We've implemented oauth + xhttp using Dispatcher Filters without calling ever a controller.

How using a model inside a component should effect the performance... I don't know who got that strange idea, sounds like not the best article you found. It is true that you should not put model layer related logic in them but you can call a model for example through the controller instance and the controllers models.

3
  • "Service logic" mostly represents the interaction between storage abstractions and domain entities. Your answer has nothing to do with it. Hell .. cake even does not separate the both aspects of model layer, since it used activerecord for its AppModel classes.
    – tereško
    Jul 26, 2013 at 14:44
  • @burzum After some reading, it seems to me that DispatcherFilter would be great for functions such as API calls. However, much of my logic is broader in nature. For example, when I receive a POST request that has failed authentication, I need to log the failed login attempt to the database. Although a simple concept, there exists quite a bit of code to instantiate a new record, validate and persist it. This kind of computation doesn't belong in the controller, as it doesn't relate directly to the request or the response. Would you still recommend DispatcherFilter for such a task?
    – emagdne
    Jul 26, 2013 at 17:03
  • 1
    Dispatcher Filters are good for middleware logic, but components are a better fit to what he is describing Dec 3, 2013 at 13:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.