【发布时间】:2015-02-12 15:16:25
【问题描述】:
在我的 Symfony 应用程序中,我尝试将控制器用作服务,以便在表格中显示我需要的数据。
这两个实体是Categories.php 和SubCategories.php。
1 个类别 可以有许多子类别,而1 个子类别 可以属于一个类别。我有一个关系ManyToOne,FK 在我的SubCategories 实体 中。因此,在我的 Symfony/Doctrine 项目中,我的 SubCategories.php 实体中有一个变量 $category。
这是我的树枝视图,她将用于在表格中搜索我的Categories.php 实体的所有数据:
<table id="dataTablesCategories">
<thead>
<tr>
<th>reference</th>
<th>id</th>
<th>name</th>
<th>description</th>
<th>Sub Categories</th>
<th>Action</th>
</tr>
</thead>
<tfoot>
<tr>
<th>reference</th>
<th>id</th>
<th>name</th>
<th>description</th>
<th>Sub Categories</th>
<th>Action</th>
</tr>
</tfoot>
<tbody>
{% for category in category %}
<tr>
<td>{{ category.reference }}</td>
<td>{{ category.id }}</td>
<td>{{ category.name}}</td>
<td>{{ category.description}}</td>
<td>
<a href="{{ path('indexSubCategories ', {'category': subCategories.category}) }}"><button class="btn btn-default btn-xs">Détails</button></a>
</td>
<td>
<a href="{{ path('editCategory ', {'name': category.name}) }}"><button class="btn btn-warning btn-xs">Modifier</button></a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
如您所见,在我的子类别列中,我有一个 , 链接到子类别的索引。事实上,我正在尝试重定向到子类别索引来管理它们。但是如果我点击这个按钮 Details,我希望 indexSubCategories 显示与我点击的类别相关的所有子类别。
像这样:
table for indexCategory
+-------------+-------------------------+
| name | Sub Categories |
+-------------+-------------------------+
| Category 1 | Details button |
| | href=indexSubcategory |
| | for Category 1 |
+-------------+-------------------------+
| Category 2 | Details button |
| | href=indexSubcategory |
| | for Category 2 |
+-------------+-------------------------+
因此,当我单击详细信息按钮时,它会显示在 indexSubcategories 中,我单击详细信息按钮的类别行的所有子类别。
这是我的类别控制器:
public function indexCategoriesAction()
{
$em=$this->getDoctrine()->getManager();
$category = $em->getRepository('MySpaceMyBundle:Categories')->findAll();
return $this->render('MySpaceMyBundle:MyFolder:indexCategories.html.twig', array('category ' => $category ));
}
这是我的子类别控制器:
public function indexSubCategoriesAction ($category)
{
$em=$this->getDoctrine()->getManager();
$subcategory = $em->getRepository('MySpaceMyBundle:SubCategories')->findOneByCategory($category);
return $this->render('MySpaceMyBundle:MyFolder:indexSubCategories.html.twig', array('subcategory' => $subcategory));
}
在indexSubCategories.html.twig 中,我在表格中做同样的事情,就像我为indexCategories.html.twig 做的一样简单。
我认为我需要使用 SubCategories 控制器作为服务来实现它?
这是我的路线文件:
# categories index #
indexCategories:
path: /managecategories
defaults: { _controller: MySpaceMyBundle:MyController:indexCategories }
requirements:
methods: GET
# Subcategories index #
indexCategories:
path: /managecategories/subcategories/{category}
defaults: { _controller: MySpaceMyBundle:MyController:indexSubCategories }
requirements:
methods: GET
我该如何真正进行?
我已经查看了here,但我该如何真正提供服务?
感谢您的帮助。
更新
我尝试将我的SubcategoriesController.php 用作服务。在我的 service.yml(同一个文件夹包)中,这是我的代码:
services:
subCategoriesDetail:
class: MyCompany\MyBundle\Controller\SubcategoriesController
我在CategoriesController.php:
public function indexCategoriesAction()
{
$em=$this->getDoctrine()->getManager();
$category = $em->getRepository('MySpaceMyBundle:Categories')->findAll();
$SubCategoriesController = $this->get('subCategoriesDetail');
return $this->render('MySpaceMyBundle:MyFolder:indexCategories.html.twig', array('categories' => $category ));
}
但是我有这个错误,这是我第一次尝试将控制器用作服务:
尝试从命名空间加载类“SubCategories” “我的公司\我的捆绑\控制器”在 C:\wamp\www\my\path\to\my\project\cache\dev\appDevDebugProjectContainer.php 第 618 行。您是否需要从另一个命名空间“使用”它?
【问题讨论】:
-
只是为了我的个人信息,你为什么要那样做?我的意思是,你为什么将控制器定义为服务?
-
@DonCallisto,事实上,我认为使用控制器作为服务可以让我访问里面的所有方法。这样我就可以从我的控制器中的另一个控制器调用方法。现在我有这个错误:
variable subcategory does not exist in twig line...并且发生错误的行是我有 detail button 和<a href="{{ path('indexSubCategories ', {'category': subCategories.category}) }}">的行 -
是的,你是对的,但这是做恕我直言的唯一理由。只是检查:P
标签: symfony service doctrine-orm controller routes