Answer the question
In order to leave comments, you need to log in
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:
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question