正如您在下面的代碼中看到的那樣,當我嘗試洗掉我的類別時。它給了我以下錯誤:
無法自動裝配“App\Controller\AdminController::deleteCategory()”的引數 $category:它參考類“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')]
類 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 >';
public $html_2 = '<li><i ></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;
}
}
uj5u.com熱心網友回復:
@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');
}
謝謝你。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/368144.html
