N
N
nepster-web2014-10-22 12:25:18
symfony
nepster-web, 2014-10-22 12:25:18

How to understand the symfony2 philosophy?

I started digging from the side of symfony2, read some Russian docks and immediately had questions about the structure.
Let's say comparing symfony2 with Yii2, can we say that the bundle is a regular module?
Then I did not quite understand where to write the main application code (let's say where in the model symphony?). That is, there is no concept of a model in symphony, but (these are not models) should be located in the Entity folder of my bundle?
And the last question, is there such a thing as widgets in symfony2 (like in Yii2) ?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2014-10-22
@nepster-web

Bundles are self-sufficient modules that encapsulate some services and other stuff. In fact, these are extensions for DependencyInjection, if very roughly.
Models are those same Entities roughly speaking. In general, there is such a thing as a domain model. This is just a data structure, the entities that the business logic operates on. The latter must be encapsulated in services (any UserManager, PostManager, etc.). In Yii, the models are mixed with the service layer and that's why you get confused.
As for the code... it's a common approach to have your own AppBundle and screw everything in it. There is also a recommended approach - do not use bundles at all. That is.... bundles should be self-sufficient and their main purpose is to reuse logic between projects. You won’t be able to reuse the business logic of the application, so it’s recommended to just write code and register it in app/Resources/config/services.yml or something like that, it’s up to you how to decide. The benefit is that you don't bother with all this garbage with bundles and you have fewer questions about the structure. And if you want to put something into the bundle - for example, authorization services that can really be reused, then no one will stop you from doing this. As a result, you will have a project structure that looks something like this:

| - app
| - var
| - src
  | - Controller
  | - Entity
  | - Bundle/
    | - MyAuthBundle/
| - web

Well, something like this. Oddly enough, this approach is not very common in the Symfony community, although it is recommended in the recently released best practice book, and in principle this structure is more than logical.
As for widgets, Symfony2 has HMVC. That is, you can make such sub-requests to other controllers inside views. You can, say, encapsulate all "widgets" as a separate controller with methods and pull them from views.
<div id="sidebar">
    {{ render(controller('AcmeArticleBundle:Article:recentArticles', {
        'max': 3
    })) }}
</div>

This gives more flexibility, inside each controller you can pull other controllers. It is possible to fasten caching at the request processing level (say, cache all subqueries according to some criteria), etc.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question