duan62819774 2015-05-05 13:41
浏览 56
已采纳

不能使用CRUDController

I'm using sonata Mongodb AdminBundle for my back-end, in my case I need to create a new action for my user model ( sending a mail to him ), I followed the documentation literally https://sonata-project.org/bundles/admin/master/doc/cookbook/recipe_custom_action.html but I got a weird error :

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

and this is my CRUDController Code :

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

any one knows the origin of this error please ?

  • 写回答

1条回答 默认 最新

  • doubeng1278 2015-05-05 14:07
    关注

    You have already imported the Controller class. And so you need to rename it in the second case. Override your use-block with next:

    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;
    

    As you see I deleted first occurrence of Controller class that is not used in your code. But if you have some other code in this file that can use "old" Controller I would recommend you to rename last Controller to BaseController and extend your class from BaseController.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器