如何在symfony 5.3.10中解决自动布线问题?

ds97pgxw  于 2023-10-24  发布在  其他
关注(0)|答案(3)|浏览(155)

正如你可以看到在下面的代码,当我试图删除我的类别.它的给予我以下错误:

无法自动连接“App\Controller\AdminController::deleteCategory()"的参数$category:它引用类“App\Entity\Category”,但不存在此类服务。

这是我在AdminController.php中创建的函数代码:

<?php

namespace App\Controller;
使用App\Utils\CategoryTreeAdminList;
使用Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
使用Symfony\Component\HttpFoundation\Response;
使用Symfony\Component\Routing\Annotation\Route;
使用App\Entity\Category;

[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');
}

}
下面是我提到categoryList的代码:

<?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;
}

}

zdwk9cvp

zdwk9cvp1#

@ soul继续使用此代码.....您可以解决此错误,并让我知道是否已解决。

#[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');

}

谢谢

vcirk6k6

vcirk6k62#

您需要运行以下命令:composer require sensio/framework-extra-bundle

2ekbmq32

2ekbmq323#

如果你使用symfony >= 5.4,你不应该安装sensio/framework-extra-bundle,因为sensio/framework-extra-bundle已经被放弃了。

相关问题