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 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,