如何正确覆盖 FOSUserBundle 的配置文件控制器? [英] How to override the FOSUserBundle's profile controller correctly?

查看:26
本文介绍了如何正确覆盖 FOSUserBundle 的配置文件控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I would like to override the ProfileController's edit action of FosUserBundle. I've created the controller in my own UserBundle, copied the edit action into it and made some changes. In this controller there is a check if the logged in user is an instanceOf UserInterFace. Apparently it's not because it throws an access denied exception when I go to /profile/edit

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

Why isn't the logged in user an instanceOf UserInterFace anymore?

控制器:

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

推荐答案

阅读你的代码片段,我会说这只是因为你不匹配 UserInterface 的完全限定命名空间.

Reading your code snippet, I would say it's simply because you don't match the full qualified namespace of UserInterface.

要么导入类:

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

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

or modify your code like this:

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

这篇关于如何正确覆盖 FOSUserBundle 的配置文件控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆