Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the correct place to share application logic in CakePHP?

I guess simple answer to the question would be a component.

Although I agree, I feel weird having to write a component for something so specific.

For example, let's say I have a table of users. When a user is created, it should form a chain reaction of events, initiating different kinds of data related to the user all around the database. I figured it would be best to avoid directly manipulating the database from different controllers and instead pack all that neatly in a method. However since some logic needs to be accesed separately, I really can't have the whole package in a single method. Instead I thought it would be logical to break it up to smaller pieces(like $userModelOrController->createNew() and $candyStorageModelOrController->createNew()) that only interact with their respective database table.

Now, if the logic is put to the model, it works great until I need to use other models. Of course it's possible, but when compared to loading models in a controller, it's not that simple. It's like a Cake developer telling me "Sure, it's possible if you want to do it that way but that's not how I would do it".

Then, if the logic is put to the controller, I can access other models really easy through $this->loadModel(), but that brings me back to the previously explained situation since I need to be able to continue the chain reaction indefinitely. Accessing other controllers from a controller is possible, but again there doesn't seem to be any direct way of doing so, so I'm guessing I'm still not doing it right.

By using a component this problem could be solved easily, since components are available to every controller I want. But like I wrote at the beginning, it feels awkward to create a component specifically for this one task. To me, components seem more like packages of extra functionality(like the core components) and not something to share controller-specific logic.

Since I'm new to this whole MVC thing, I could've completely misunderstood the concept. Once again, I would be thankful if someone pointed me to the right direction :)

like image 701
jpeltoniemi Avatar asked Jun 17 '10 19:06

jpeltoniemi


1 Answers

Whenever I am faced with such difficult scenarios, I stare at CakePHP Events System for an infinite number of hours.

I've so far managed to avoid it somehow (mostly due to lack of courage), but I think it's worth looking into, since it is obviously designed to jump the MVC "walls" to some extent.

Another possible solution is the Finite State Machine behavior, which allows you to push the logic towards the Model more cleanly and build your Controller+Component around it. The only fallback (IMHO) of this Behavior is that it doesn't allow for multiple state fields on a single Model.

A more factual representation of such a problem might yield better solutions, since many of us went down this road and it could be a valuable example.

like image 143
Vanja D. Avatar answered Sep 23 '22 17:09

Vanja D.