M
M
MasterCopipaster2020-05-25 10:26:36
symfony
MasterCopipaster, 2020-05-25 10:26:36

Symfony 4 how to write menu middleware?

Hello everyone, tell me how to solve the following problem, I have twig templates that look like this


├── base.html.twig
├── email
│ └── index.html.twig
├── footer.html.twig
├── header.html.twig
├── index
│ ├── index.html.twig
│ ├── modal_load_file.html.twig
│ └── modal_load_link.html.twig
└── output
└── index.html.twig

In the header.html.twig file, I have a menu like this:
This is the controller for the main page

/**
     * @Route("/", name="index", methods={"GET","HEAD"})
     */
    public function index()
    {
       $menu = $this->getMenu();
       // some code
              return $this->render('index/index.html.twig', [
            // some vars
            'menu' => $menu
        ]);
    }

base.html.twig looks like this
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
        <title>{% block title %}Welcome!{% endblock %}</title>
        <link rel="stylesheet" href="{{ asset('css/bootstrap.min.css') }}"/>
        <link rel="stylesheet" href="{{ asset('css/datepicker.min.css') }}"/>
        <link rel="stylesheet" href="{{ asset('css/main.css') }}"/>
        <script src="{{ asset('js/jquery-3.4.1.min.js') }}" type="text/javascript"></script>
        <script src="{{ asset('js/popper.min.js') }}" type="text/javascript"></script>
        <script src="{{ asset('js/bootstrap.min.js') }}" type="text/javascript"></script>
        <script src="{{ asset('js/datepicker.min.js') }}" type="text/javascript"></script>
        <script src="{{ asset('js/validator.form.js') }}" type="text/javascript"></script>
        <script src="{{ asset('js/download.files.js') }}" type="text/javascript"></script>
        <script src="{{ asset('js/download.link.js') }}" type="text/javascript"></script>
        <script src="{{ asset('js/main.js') }}" type="text/javascript"></script>


        {% block stylesheets %}{% endblock %}
    </head>
    <body>
    {{ include('header.html.twig') }}
        {% block body %}{% endblock %}
        {% block javascripts %}{% endblock %}
    </body>
    {{ include('footer.html.twig') }}
<script>

</script>
</html>

Here the index page works, everything is ok, but when I go to another controller,
the menu does not work (which is generally logical)
for example

└── output
└── index.html.twig

base cannot show the page because the menu was not passed as a parameter to the template, I believe that in each controller to pass the menu separately as a parameter to twig is a dich who knows how to do it so that when trying to go to any page (well, or pre-selected group of pages) return menu method
automatically pass this data to twig? In laravel, this is done through middleware in symfony, no matter how much I googled, I did not understand how to do it correctly. Can someone tell me how to solve this issue nicely?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Fedorov, 2020-05-25
@MasterCopipaster

Twig Extension
Create your extension by passing your services to it (repository for example)
In the getFunctions() method, register your twig function
Register it with a tag twig.extension
Call it in your templates

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question