正如您在下面的代码中看到的,当我试图删除我的类别时。它给了我以下错误:
无法自动连接"的参数$category;App\Controller\AdminController::deleteCategory((":它引用类";应用程序\实体\类别"但是不存在这样的服务
这是我在AdminController.php中创建的函数代码:
<?php
命名空间App\Controller;
使用App\Utils\CategoryTreeAdminList;
使用Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
使用Symfony\Component\HttpFoundation\Response;
使用Symfony\Component\Routing\Annotation\Route;
使用应用程序\实体\类别;
#[路线('/admin'(]
类AdminController扩展AbstractController{
#[Route('/delete-category/{id}', name: 'delete_category')]
public function deleteCategory(Category $category): Response
{
$entityManager = $this->getDoctrine()->getManager();
$entityManager->remove($category);
$entityManager->flush();
return $this->redirectToRoute('categories');
}
}
以下是我提到的分类列表的代码:
<?php
命名空间App\Utils;
使用App\Utils\AbstractClasses\CategoryTreeAbstract;
类CategoryTreeAdminList扩展了CategoryTreeAbstract{
public $html_1 = '<ul class="fa-ul text-left">';
public $html_2 = '<li><i class="fa-li fa fa-arrow-right"></i> ';
public $html_3 = '<a href="';
public $html_4 = '">';
public $html_5 = '</a> <a onclick="return confirm('Are you sure?');" href="';
public $html_6 = '">';
public $html_7 = '</a>';
public $html_8 = '</li>';
public $html_9 = '</ul>';
public function getCategoryList(array $categories_array)
{
$this->categorylist .= $this->html_1;
foreach ($categories_array as $value) {
$url_edit = $this->urlgenerator->generate('edit_category', ['id' => $value['id']]);
$url_delete = $this->urlgenerator->generate('delete_category', ['id' => $value['id']]);
$this->categorylist .= $this->html_2 . $value['name'] .
$this->html_3 . $url_edit . $this->html_4 . ' Edit' .
$this->html_5 . $url_delete . $this->html_6 . 'Delete' .
$this->html_7;
if (!empty($value['children'])) {
$this->getCategoryList($value['children']);
}
$this->categorylist .= $this->html_8;
}
$this->categorylist .= $this->html_9;
return $this->categorylist;
}
}
@sadam继续执行此代码。。。。。你可以用这个解决你的错误,如果解决了,请告诉我。
#[Route('/delete-category/{id}', name: 'delete_category')]
public function deleteCategory($id): Response
{
$entityManager = $this->getDoctrine()->getManager();
$category = $entityManager->getRepository(Category::class)->find($id);
$entityManager->remove($category);
$entityManager->flush();
return $this->redirectToRoute('categories');
}
谢谢。
您需要运行以下命令:composer require sensio/framework-extra-bundle
如果使用symfony>=5.4您不应该安装sensio/framework-extra-bundle
,因为sensio/framework-extra-bundle
已被放弃。见Ayush 的上述解决方案