[英]Symfony2 controller navbar
i would put a controller for my navbar and i would use a query to get a variable from my database.. I don't have a controller and i create it in this way: 我将为我的导航栏放置一个控制器,并使用查询从数据库中获取变量。.我没有控制器,我以这种方式创建它:
<?php
namespace Dt\EcBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
class NavbarController extends Controller {
public function navbarAction(Request $request) {
$prova = "ciao";
return $this->render('DtEcBundle:Header:navbar.html.twig',array(
"prova" => $prova,
));
}
}
Now i put my render controller in the body of : " {# app/Resources/views/base.html.twig #} " 现在,我将渲染控制器放在以下主体中:“ {#app / Resources / views / base.html.twig#} ”
{{ render(controller('DtEcBundle:Navbar:navbar', { 'prova': prova })) }}
I follow this but i don t understand the error: " http://symfony.com/doc/current/book/templating.html#embedding-controllers " 我遵循此步骤,但是我不理解错误:“ http://symfony.com/doc/current/book/templating.html#embedding-controllers ”
I Get this error Variable "prova" does not exist in DtEcBundle:Header:navbar.html.twig at line 5 but if i write the code in navbar.html.twig give me equals error.. If i remove the variable and i write only 我收到此错误第五行的DtEcBundle:Header:navbar.html.twig中不存在变量“ prova”,但是如果我在navbar.html.twig中编写代码,则给我等号错误。只要
{{ render(controller('DtEcBundle:Navbar:navbar')) }}
Give me a server error number 500 oo. 给我一个服务器错误号500 oo。 How can i do for use my controller only in navbar.html.twig??
我该如何仅在navbar.html.twig中使用我的控制器?
The error is the code: 错误是代码:
{{ render(controller('DtEcBundle:Navbar:navbar', { 'prova': prova })) }}
the prova variable doesn't exists in twig, the controller is fine. prova变量在树枝中不存在,控制器很好。 I if you want put the var from twig to controller:
我如果你想把变种从树枝到控制器:
/**
* @Route("/prova/{prova}", name="prova")
*/
public function navbarAction(Request $request,$prova) {
return $this->render('DtEcBundle:Header:navbar.html.twig',array(
"prova" => $prova,
));
}
and twig: 和树枝:
{% set prova = 'foo' %}
{{ render(controller('DtEcBundle:Navbar:navbar', { 'prova': prova })) }}
navbarAction doesn't take prova variable as a parameter, so why are you passing it there in a base template? navbarAction不使用prova变量作为参数,那么为什么要在基本模板中传递它呢?
I think that action should fetch these data from db. 我认为该操作应从db获取这些数据。
In that case, using: 在这种情况下,请使用:
{{ render(controller('DtEcBundle:Navbar:navbar')) }}
seems to be ok, and error is somewhere else. 似乎还可以,但错误仍然存在。
If you get a 500, check logs to tell us what's exactly wrong. 如果您得到500,请检查日志以告诉我们到底出了什么问题。
And format your code, it's barely readable. 并格式化您的代码,它几乎不可读。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.