E
E
Eugene2017-07-09 18:15:11
symfony
Eugene, 2017-07-09 18:15:11

Analogue of 1C-Bitrix components in Symfony?

Good day everyone.
Now I am getting acquainted with Symfony, before that I worked with 1C-Bitrix for about 2 years.
Because in 1C-Bitrix, the role of controllers, roughly speaking, is performed by components, the following case is interesting:

  1. There is a site template consisting of two columns.
  2. The left column is a sidebar with the site menu and a brief announcement of the latest news.
  3. The right column is the content area, i.e. its content changes depending on the current section of the site.

So, in Bitrix it was possible to implement a list of the latest news through a component, and place it in the site template. After that, when creating a new page, it was enough to deal with the content part of the page, the sidebar was rendered by itself and did not require attention. It turns out that, in a sense, it was possible to use several controllers on one page at the same time.
Now a similar case needs to be done in Symfony, but since here is inheritance in Twig templates, it turns out that in each controller you need to select a list of the latest news?
How is it customary in Symfony to solve such a problem? Use some kind of generic model that needs to be called in every controller and pass the result to the template? Or is it possible somehow once, in the parent template, to specify how to select news?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Pavlov, 2017-07-09
@summon7300

In Symfony, usually the main code runs in services, and the controller calls the desired service. A big plus - once I created the code for working with the entity, and you can use it anywhere - in any controller, in another service, in a template. The whole year of work with the entity - in one place (in one service).
1) Make a service that builds a list of the latest news (the same service uses a cache).
2) Create a Twig extension that adds a function for getting a list of the latest news (getting from the created service). For example, create a "lastNews" function. Set up a link to the news service through the container (in services.yml, or where you make services there). (links for creating functions: 1 , 2 )
3) In the main template (app/Resources/views/layout.html.twig) call the created function. Simplified like this:

{# app/Resources/views/base.html.twig #}
<!DOCTYPE html>
<html lang="ru">
    <head>
    </head>
    <body>
        {% block body%}{% endblock %}
        {% block javascripts %}{% endblock %}
    </body>
</html>

{# app/Resources/views/layout.html.twig #}
{% extends 'base.html.twig' %}

{% block body %}
<div class="container">
    <div class="sidebar">
        {% block sidebar %}
            <ul class="sidebar-menu">
            {% for item in lastNews() %}
              <li><a href="{{ item.href }}">{{ item.title }}</a></li>
            {% endfor %}
            </ul>
        {% endblock %}
    </div>
    <div class="content">
        {% block content %}
        {% endblock %}
    </div>
</div>
{% endblock %}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question