gpt4 book ai didi

How to solve autowire problem in symfony 5.3.10?(如何在symfony 5.3.10中解决自动布线问题?)

转载 作者:bug小助手 更新时间:2023-10-24 23:41:35 30 4
gpt4 key购买 nike



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.

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


This is the code of function I have created in AdminController.php:

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


<?php

namespace App\Controller;

命名空间应用程序\控制器;


use App\Utils\CategoryTreeAdminList;

使用App\Utils\CategoryTreeAdminList;


use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

使用Symfony\Bundle\FrameworkBundle\Controller\AbstractController;


use Symfony\Component\HttpFoundation\Response;

使用symfony\Component\HttpFoundation\Response;


use Symfony\Component\Routing\Annotation\Route;

使用symfony\Component\Routing\Annotation\Routing;


use App\Entity\Category;

使用应用程序\实体\类别;


#[Route('/admin')]

#[Route(‘/admin’)]


class AdminController extends AbstractController
{

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

}

}


Following is the code where I have mentioned categoryList:

下面的代码中我提到了categyList:


<?php

namespace App\Utils;

命名空间App\Utils;


use App\Utils\AbstractClasses\CategoryTreeAbstract;

使用App\Utils\AbstractClasses\CategoryTreeAbstract;


class CategoryTreeAdminList extends 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;
}

}

}


更多回答

Check the entity Category, its path, its name space

检查实体类别、其路径、名称空间

Why are you building html outside of a view?

为什么要在视图之外构建html?

Did you add the Symfony SensioFrameworkExtraBundle to your project? I believe that's what's needed to convert the {id} in your route to the Category object. symfony.com/bundles/SensioFrameworkExtraBundle/current/…

您是否将symfony SensioFrameworkExtraBundle添加到您的项目中?我相信这就是将路由中的{id}转换为Category对象所需的内容。Symfony.com/bundles/SensioFrameworkExtraBundle/current/…

this error is not related to any of the bundle or building html, this comes under the passing the classes as argument or injecting the entities as argument, i don't know why but its can solved as; -passing the $id as argument - writing this code, "$categories = $entityManager->getRepository(Category::class)->find($id);" after this code $entityManager = $this->getDoctrine()->getManager(); it's may solve your problem, get a look with my answer....

此错误与任何捆绑包或构建的html无关,它位于将类作为参数传递或将实体作为参数注入之下,我不知道为什么,但它可以解决为;-将$id作为参数传递-编写此代码,“$Categories=$entityManager->getRepository(Category::class)->find($id);”后此代码$EntityManager=$This->getDoctrine()->getManager();它可能会解决您的问题,请查看我的答案...

@Ayush That (converting the id into an object) is exactly what the SensioFrameWorkExtraBundle does. This bundle is very commonly used and uses the exact syntax as posted in the question above (id in the route, object as a parameter in the method). So while your fix works, I still think it could have been more easily solved by installing the bundle (and no other edits are required!).

@Ayush(将id转换为对象)正是SensioFrameWorkExtraBundle所做的事情。这个捆绑包非常常用,并且使用上面问题中发布的完全相同的语法(路由中的id,方法中的参数Object)。因此,尽管您的修复有效,我仍然认为通过安装包可以更容易地解决这个问题(并且不需要其他编辑!)。

优秀答案推荐

@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.

谢谢。



You'll need to run this command: composer require sensio/framework-extra-bundle

您需要运行以下命令:Composer需要Sensio/framework-Extra-Bundle



If you use symfony >= 5.4 you should not install sensio/framework-extra-bundle because sensio/framework-extra-bundle has been abandoned. See the solution above by Ayush

如果您使用symfony>=5.4,则不应该安装sensio/framework-Extra-Bundle,因为Sensio/framework-Extra-Bundle已被放弃。请参阅Ayush提供的上述解决方案


更多回答

Just to reiterate from the comments, yes the above code works but it should not be needed. The param converter capability takes care of simple object fetches. Plus, the getDoctrine method is deprecated in 5.4 and should not longer be used.

只是从评论中重申一下,是的,上面的代码可以工作,但不应该需要它。参数转换器功能负责简单的对象获取。另外,在5.4中不推荐使用getDoctrine方法,因此不应该再使用它。

Actually my preferred method is working in my latest version too so i didn't care about this but you goes right and your changes also works good. thanks. @Cerad

实际上,我更喜欢的方法也适用于我的最新版本,所以我并不关心这一点,但您做得对,您的更改也很有效。谢谢。@CERAD

@Cerad thanks for your concern your suggestion is quite helpful for my problem,,

@CERAD感谢您的关心,您的建议对我的问题很有帮助,,

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com