简体   繁体   中英

Load list of active users from database with symfony without using a controller

I'm making a presonification menu on my navbar so that i can access from any page i don't want to use the getRepository inside a controller and pass to the frontend.

Is there a way on symfony 4 that I can get all the users from my database and call it to front? like the app.session.user (to get info on the logged user) for example?

Thanks!

As Cerad mentioned, you can embed a Controller in your templates : https://symfony.com/doc/current/templates.html#embedding-controllers

Instead of doing {% include 'base/navbar.html.twig' %} you will do

In your template :

{{ render(controller('App\\Controller\\BaseController::renderNavbar')) }}

In BaseController

public function renderNavbar(UserRepository $userRepository) {
    return $this->render('base/navbar.html.twig', [
        'activeUsers' => $userRepository->findActiveUsers(),
    ]);
}

It's not a route, it's simply a function that renders html, so you don't need to add annotations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM