问题
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 :)
回答1:
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.
回答2:
It is deifinitley bugging that CakePHP does not support easly
- accessing another Model from a Model
- accessing a Component from a Model
- accessing any Model from a Component
- having a function accessible both from Controllers and Views (something between Components and Helpers), or accessing a Component from a Helper (or vica-versa)
I mostly ended up adding function to the superclass AppController.
回答3:
It is clearly stated in the cookbook: "Components are packages of logic that are shared between controllers. If you find yourself wanting to copy and paste things between controllers, you might consider wrapping some functionality in a component."
回答4:
You can use the AppModel if you want your models to share some logic. If you need to access different models in your model, you can use App::import()
or ClassRegistry::init
. Now if you want the logic to be available only to some models of your choice, you can use a Behavior
or create a different AppModel where only your selected models will inherit.
回答5:
Probably a better way to implement this is within a plugin. Plugins allow you to encapsulate entire functionality across all parts of MVC into a package which can be pulled in anywhere in your code via the plugin's Helpers, Components, Behaviors. This way,
- you can put the functionality where its most appropriate,
- the logic is kept separate from the rest of your app logic and
- you can enable/disable this functionality as needed.
来源:https://stackoverflow.com/questions/3064883/whats-the-correct-place-to-share-application-logic-in-cakephp