简体   繁体   English

在不使用控制器的情况下使用 symfony 从数据库加载活动用户列表

[英]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.我正在我的导航栏上制作一个预声化菜单,以便我可以从任何页面访问我不想使用控制器内的 getRepository 并传递给前端。

Is there a way on symfony 4 that I can get all the users from my database and call it to front?在 symfony 4 上有没有办法从我的数据库中获取所有用户并将其调用到前面? like the app.session.user (to get info on the logged user) for example?例如app.session.user (获取登录用户的信息)?

Thanks!谢谢!

As Cerad mentioned, you can embed a Controller in your templates : https://symfony.com/doc/current/templates.html#embedding-controllers正如 Cerad 提到的,你可以在你的模板中嵌入一个控制器: https ://symfony.com/doc/current/templates.html#embedding-controllers

Instead of doing {% include 'base/navbar.html.twig' %} you will do而不是做{% include 'base/navbar.html.twig' %}你会做

In your template :在您的模板中:

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

In BaseController在 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.不是路由,只是一个渲染html的函数,所以不需要加注解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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