Symfony2 MVC: where does my code belong?

前端 未结 3 380
独厮守ぢ
独厮守ぢ 2020-12-09 06:18

I\'m seeking clarification on whether to put code in a controller, an entity or to make a service.

I have \'cardset\' and \'card\' objects (where many of the latter

3条回答
  •  不思量自难忘°
    2020-12-09 07:16

    • Symfony2 is NOT an MVC structure (as said by Fabien himself) precisely because it gives all the V (twig) and C (controllers) but does NOT give the M part. The M part is "free" to be built as you want.

    • There is a major confussion, people "think" that Doctrine IS the model. But that's not true. What we do is TWO directories in the Bundle, one called "Document" for the Doctrine-ODM classes and one called "Model" where the "business logic" reside.

    Personally I see that $card->makePDF() makes sense...

    But $card should be a "model card", which inherits or has an underlying object "data card" which is the doctrine class.

    You can play with inheritance, or with interfaces, with creators or whatever you want to relate "model-card" to "data-card" but the key is that "doctrine is not BUSINESS model" it is simply a persistance layer and your model are "plain classes" that you can build to wrap your data inside and make the controllers to consume the model, not the data.

提交回复
热议问题