我正在尝试使用以下代码行列出我主页树枝中的所有类别:
{% extends 'base.html.twig' %}
{% block body %}
<!-- Page Content -->
<div class="container">
<div class="row">
<div class="col-md-3">
<p class="lead">Categories</p>
<div class="list-group">
{% for category in categories %}
<a href="#" class="list-group-item">{{ category.name }}</a>
{% endfor %}
</div>
</div>
<div class="col-md-9">
<div class="row carousel-holder">
<div class="col-md-12">
<div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
<ol class="carousel-indicators">
<li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li>
<li data-target="#carousel-example-generic" data-slide-to="1"></li>
<li data-target="#carousel-example-generic" data-slide-to="2"></li>
</ol>
<div class="carousel-inner">
<div class="item active">
<img class="slide-image" src="https://files.illinoispolicy.org/wp-content/uploads/2016/09/Garbage-Politicians-Jesse-White-800x300.png" alt="">
</div>
<div class="item">
<img class="slide-image" src="https://files.illinoispolicy.org/wp-content/uploads/2016/07/garbage-politicians-michael-madigan-800x300.png" alt="">
</div>
<div class="item">
<img class="slide-image" src="https://files.illinoispolicy.org/wp-content/uploads/2015/03/lion-meat-act-2-800x300.jpg" alt="">
</div>
</div>
<a class="left carousel-control" href="#carousel-example-generic" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left"></span>
</a>
<a class="right carousel-control" href="#carousel-example-generic" data-slide="next">
<span class="glyphicon glyphicon-chevron-right"></span>
</a>
</div>
<br>
</div>
</div>
<div class="row">
<div class="col-sm-4 col-lg-4 col-md-4">
<div class="thumbnail">
<img src="http://placehold.it/320x150" alt="">
<div class="caption">
<h4 class="pull-right">$24.99</h4>
<h4><a href="#">First Product</a>
</h4>
<p>See more snippets like this online store item at <a target="_blank" href="http://www.bootsnipp.com">Bootsnipp - http://bootsnipp.com</a>.</p>
</div>
<div class="ratings">
<p class="pull-right">15 reviews</p>
<p>
<span class="glyphicon glyphicon-star"></span>
<span class="glyphicon glyphicon-star"></span>
<span class="glyphicon glyphicon-star"></span>
<span class="glyphicon glyphicon-star"></span>
<span class="glyphicon glyphicon-star"></span>
</p>
</div>
</div>
</div>
<div class="col-sm-4 col-lg-4 col-md-4">
<div class="thumbnail">
<img src="http://placehold.it/320x150" alt="">
<div class="caption">
<h4 class="pull-right">$64.99</h4>
<h4><a href="#">Second Product</a>
</h4>
<p>This is a short description. Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
</div>
<div class="ratings">
<p class="pull-right">12 reviews</p>
<p>
<span class="glyphicon glyphicon-star"></span>
<span class="glyphicon glyphicon-star"></span>
<span class="glyphicon glyphicon-star"></span>
<span class="glyphicon glyphicon-star"></span>
<span class="glyphicon glyphicon-star-empty"></span>
</p>
</div>
</div>
</div>
<div class="col-sm-4 col-lg-4 col-md-4">
<div class="thumbnail">
<img src="http://placehold.it/320x150" alt="">
<div class="caption">
<h4 class="pull-right">$74.99</h4>
<h4><a href="#">Third Product</a>
</h4>
<p>This is a short description. Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
</div>
<div class="ratings">
<p class="pull-right">31 reviews</p>
<p>
<span class="glyphicon glyphicon-star"></span>
<span class="glyphicon glyphicon-star"></span>
<span class="glyphicon glyphicon-star"></span>
<span class="glyphicon glyphicon-star"></span>
<span class="glyphicon glyphicon-star-empty"></span>
</p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
{% endblock %}
这是我的控制器中的功能:
public function listCategoriesAction()
{
$em = $this->getDoctrine()->getManager();
$categoryRepository = $em->getRepository('AppBundle:Category');
$categories = $categoryRepository->findAll();
return $this->render('homepage/homepage.html.twig', array(
'categories' => $categories
));
}
它告诉我变量类别不存在。我想这是因为我试图在1枝中列出来自不同控制器的许多功能,但我不知道如何解决它。 这是功能上方的路线。我第二次使用它,是问题吗?
/**
* @Route("/home", name="homepage")
* @return \Symfony\Component\HttpFoundation\Response
*/