如何正确覆盖FOSUserBundle的配置文件控制器?

时间:2012-02-11 10:00:58

标签: symfony

我想覆盖ProfileController的FosUserBundle编辑操作。我在自己的UserBundle中创建了控制器,将编辑操作复制到其中并进行了一些更改。在此控制器中,检查登录用户是否为UserInterFace的instanceO。显然,这并不是因为当我转到/ profile / edit

时它会抛出拒绝访问的异常

为什么登录用户不再是一个instanceOf UserInterFace?

控制器:     

namespace Tennisconnect\UserBundle\Controller;

use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use FOS\UserBundle\Controller\ProfileController as BaseController;

class ProfileController extends BaseController
{
/**
     * Edit the user
     */
    public function editAction()
    {
        $user = $this->container->get('security.context')->getToken()->getUser();

        if (!is_object($user) || !$user instanceof UserInterface) {
            throw new AccessDeniedException('This user does not have access to this     section.');
        }

        $form = $this->container->get('fos_user.profile.form');
        $formHandler = $this->container->get('fos_user.profile.form.handler');

        $process = $formHandler->process($user);
        if ($process) {
            $user->upload();
            $this->setFlash('fos_user_success', 'profile.flash.updated');

            return new RedirectResponse($this->container->get('router')->generate('fos_user_profile_show'));
        }

        return $this->container->get('templating')->renderResponse(
        'FOSUserBundle:Profile:edit.html.'.$this->container->getParameter('fos_user.template.engine'),
        array('form' => $form->createView(), 'theme' => $this->container->getParameter('fos_user.template.theme'))
        );
    }
}

2 个答案:

答案 0 :(得分:4)

阅读你的代码片段,我想说这只是因为你不匹配UserInterface的完整限定名称空间。

使用以下内容导入课程:

use Symfony\Component\Security\Core\User\UserInterface;

或修改你的代码:

if (!is_object($user) || !$user instanceof  Symfony\Component\Security\Core\User\UserInterface) {

答案 1 :(得分:3)

更清洁的解决方案是在捆绑类中创建一个新的Bundle“MyFOSBundle”:

getParent()
{
    return "FosBundle";
}

然后在同一位置编写要覆盖的文件。

http://symfony.com/doc/master/cookbook/bundles/inheritance.html