不能使用 CRUDController

Can not use CRUDController

我在后端使用 sonata Mongodb AdminBundle,在我的例子中,我需要为我的用户模型创建一个新操作(向他发送邮件),我从字面上遵循了文档 https://sonata-project.org/bundles/admin/master/doc/cookbook/recipe_custom_action.html 但是我得到了一个奇怪的错误:

Compile Error: Cannot use Sonata\AdminBundle\Controller\CRUDController as Controller because the name is already in use

这是我的 CRUDController 代码:

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Session\UserBundle\Document\User;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Sonata\AdminBundle\Controller\CRUDController as Controller;

class CrudController extends Controller
{
    public function inscriptionAction()
    {
        $mail = 'towho@someone.com';
        $pinCode = '1klm8';
        $sender = 'Mymail@gmail.com';

        $dm = $this->get('doctrine_mongodb')->getManager();
        $userManager = $this->container->get('fos_user.user_manager');

        $user = $userManager->createUser();
        $user->setEmail($mail);
        $user->setUsername($mail);
        $user->setPlainPassword($pinCode);
        $user->setEnabled(true);
        $userManager->updateUser($user, true);


        $message = \Swift_Message::newInstance()
            ->setSubject('Test de recrutement')
            ->setFrom($sender)
            ->setTo($mail)
            ->setCharset('utf-8')
            ->setContentType('text/html')
            ->setBody(
                $this->renderView(
                    'ATSQuizzBundle:Default:SwiftLayout/createUser.html.twig',
                    array('user' => $user, 'pinCode' => $pinCode)
                )
            );

        $this->get('mailer')->send($message);
        $this->addFlash('sonata_flash_success', 'mail sent to the candidate');

        return new RedirectResponse($this->admin->generateUrl('list'));
    }
}

有人知道这个错误的来源吗?

您已经导入了 Controller class。所以你需要在第二种情况下重命名它。用 next 覆盖你的 use-block:

use Session\UserBundle\Document\User;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Sonata\AdminBundle\Controller\CRUDController as Controller;

如您所见,我删除了第一个未在您的代码中使用的 Controller class。但是如果你在这个文件中有一些其他的代码可以使用 "old" Controller 我建议你将 last Controller 重命名为 BaseController 并将你的 class 从BaseController.