如何解决 symfony 5.3.10 中的自动装配问题?
How to solve autowire problem in symfony 5.3.10?
正如您在下面的代码中看到的,当我试图删除我的类别时。它给我以下错误:
无法自动装配“App\Controller\AdminController::deleteCategory()”的参数 $category:它引用 class“App\Entity\Category”但不存在此类服务。
这是我在AdminController.php中创建的函数代码:
<?php
命名空间App\Controller;
使用App\Utils\CategoryTreeAdminList;
使用Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
使用Symfony\Component\HttpFoundation\Response;
使用Symfony\Component\Routing\Annotation\Route;
使用App\Entity\Category;
#[路由('/admin')]
class 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');
}
}
以下是我提到 categoryList 的代码:
<?php
命名空间App\Utils;
使用 App\Utils\AbstractClasses\CategoryTreeAbstract;
class 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;
}
}
@saddam 继续使用这段代码......你可以用这个解决你的错误,如果它解决了请告诉我。
#[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');
}
谢谢。
正如您在下面的代码中看到的,当我试图删除我的类别时。它给我以下错误:
无法自动装配“App\Controller\AdminController::deleteCategory()”的参数 $category:它引用 class“App\Entity\Category”但不存在此类服务。
这是我在AdminController.php中创建的函数代码:
<?php
命名空间App\Controller;
使用App\Utils\CategoryTreeAdminList;
使用Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
使用Symfony\Component\HttpFoundation\Response;
使用Symfony\Component\Routing\Annotation\Route;
使用App\Entity\Category;
#[路由('/admin')]
class 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');
}
}
以下是我提到 categoryList 的代码:
<?php
命名空间App\Utils;
使用 App\Utils\AbstractClasses\CategoryTreeAbstract;
class 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;
}
}
@saddam 继续使用这段代码......你可以用这个解决你的错误,如果它解决了请告诉我。
#[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');
}
谢谢。