As you can see in the code below, when I am trying to delete my category. It's give me following errors:
Cannot autowire argument $category of "App\Controller\AdminController::deleteCategory()": it references class "App\Entity\Category" but no such service exists.
This is the code of function I have created in AdminController.php:
<?php
namespace App\Controller;
use App\Utils\CategoryTreeAdminList;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use App\Entity\Category;
#[Route('/admin')]
class AdminController extends 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');
}
}
Following is the code where I have mentioned categoryList:
<?php
namespace App\Utils;
use App\Utils\AbstractClasses\CategoryTreeAbstract;
class CategoryTreeAdminList extends 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;
}
}
CodePudding user response:
@saddam Go ahead with this code..... you'll may solve your error with this and let me know if its solved.
#[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');
}
Thank You.