【问题标题】:How to override the FOSUserBundle's profile controller correctly?如何正确覆盖 FOSUserBundle 的配置文件控制器?
【发布时间】:2012-08-10 21:05:25
【问题描述】:

我想覆盖 ProfileController 的 FosUserBundle 的编辑操作。我在自己的 UserBundle 中创建了控制器,将编辑操作复制到其中并进行了一些更改。在此控制器中,检查登录用户是否为 instanceOf UserInterFace。显然这不是因为当我转到 /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'))
        );
    }
}

【问题讨论】:

    标签: symfony


    【解决方案1】:

    阅读您的代码 sn-p,我想说这仅仅是因为您不匹配 UserInterface 的完整限定命名空间。

    要么导入类:

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

    或者像这样修改你的代码:

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

    【讨论】:

      【解决方案2】:

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

      getParent()
      {
          return "FosBundle";
      }
      

      然后您将要覆盖的文件写入同一位置。

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

      【讨论】:

        猜你喜欢
        • 2015-10-08
        • 2020-11-11
        • 2016-03-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-11-19
        • 2013-09-15
        相关资源
        最近更新 更多