Symfony2嵌入了所有FOSUserBundle表单

时间:2014-03-19 13:43:45

标签: forms symfony fosuserbundle

我试图将所有FOSUserBundle表单嵌入到布局左侧的侧边栏中。

这是我的src/My/MainBundle/Resources/config/routing.yml

index:
    pattern: /
    defaults: { _controller: MyMainBundle:Main:index }

login:
    pattern: /login
    defaults: { _controller: MyMainBundle:Main:index }

resetting_request:
    pattern: /resetting/request
    defaults: { _controller: MyMainBundle:Main:index }

resetting_send_email:
    pattern: /resetting/send-email
    defaults: { _controller: MyMainBundle:Main:index }

src/My/MainBundle/Resources/views/layout.html.twig中,我(其中包括):

<div id="sidebar">
    {% if is_granted('IS_AUTHENTICATED_REMEMBERED') %}
        <p>You are logged in blah blah blah</p>
    {% elseif app.request.attributes.get('_route') == 'resetting_request' %}
        {{ render(controller('FOSUserBundle:Resetting:request')) }}
    {% elseif app.request.attributes.get('_route') == 'resetting_send_email' %}
        {{ render(controller('FOSUserBundle:Resetting:sendEmail')) }}
    {% else %}
        {{ render(controller('FOSUserBundle:Security:login')) }}
    {% endif %}
</div>

登录表单工作正常,它提交到/ login_check,然后重定向到请求的页面,如果登录名和密码是正确的,或者,如果登录名或密码错误,它会重定向到/ login(在如果它正确显示侧栏中的表单,并显示相应的错误消息。

我在使用&#34;忘记密码时遇到问题?&#34;但是,形式。

它显示在侧边栏中,并提交/重置/发送电子邮件,但它不会重定向到任何内容。然后它告诉我用户名或电子邮件&#34;&#34;不存在。

如果我评论&#34; resetting_send_email&#34;路线,然后&#34;发送电子邮件&#34;动作是有效的,但输出不在侧边栏中(事实上,它周围甚至没有布局)。

有什么想法吗?

3 个答案:

答案 0 :(得分:1)

FOSUserBundle:Resetting:sendEmail期待用户名或电子邮件作为POST。

public function sendEmailAction(Request $request)
{
    $username = $request->request->get('username');

    /** @var $user UserInterface */
    $user = $this->container->get('fos_user.user_manager')
                ->findUserByUsernameOrEmail($username);

    if (null === $user) {
        return $this->container->get('templating')->renderResponse('bla blah');
    }

当您渲染控制器时,您向控制器发出新请求而没有POST,因此没有电子邮件或用户名。

如果没有POST,脚本找不到user,这意味着它会在第一次检查时中断。

答案 1 :(得分:0)

我尝试使用controller events,但这还不够。

我必须override the FOSUserBundle controllers

所以,我创建了一个新的包(MyUserBundle),FOSUserBundle的子包,如链接中所述,这是我的控制器:

// src/My/UserBundle/Controller/ResettingController.php

namespace My\UserBundle\Controller;

use \Symfony\Component\HttpFoundation\Request;
use FOS\UserBundle\Controller\ResettingController as BaseController;
use Symfony\Component\HttpFoundation\RedirectResponse;

class ResettingController extends BaseController
{
    /**
     * Request reset user password: submit form and send email
     */
    public function sendEmailAction(Request $request)
    {
        //$username = $request->request->get('username'); // Since this action was called in a twig file by a "{{ render(controller(...)) }}", the "request" attribute is empty.
        $username = $_POST['username']; // But $_POST still works

        /** @var $user UserInterface */
        $user = $this->container->get('fos_user.user_manager')->findUserByUsernameOrEmail($username);

        if (null === $user) {
            return $this->container->get('templating')->renderResponse('FOSUserBundle:Resetting:request.html.'.$this->getEngine(), array('invalid_username' => $username));
        }

        if ($user->isPasswordRequestNonExpired($this->container->getParameter('fos_user.resetting.token_ttl'))) {
            return $this->container->get('templating')->renderResponse('FOSUserBundle:Resetting:passwordAlreadyRequested.html.'.$this->getEngine());
        }

        if (null === $user->getConfirmationToken()) {
            /** @var $tokenGenerator \FOS\UserBundle\Util\TokenGeneratorInterface */
            $tokenGenerator = $this->container->get('fos_user.util.token_generator');
            $user->setConfirmationToken($tokenGenerator->generateToken());
        }

        $this->container->get('fos_user.mailer')->sendResettingEmailMessage($user);
        $user->setPasswordRequestedAt(new \DateTime());
        $this->container->get('fos_user.user_manager')->updateUser($user);

        //return new RedirectResponse($this->container->get('router')->generate('fos_user_resetting_check_email',
        //    array('email' => $this->getObfuscatedEmail($user))
        //)); // Throws an exception (because this action was called by a "{{ render(controller(..)) }}").

        //header('Location: '.$this->container->get('router')->generate('fos_user_resetting_check_email', array('email' => $this->getObfuscatedEmail($user))));
        //exit; // The redirect works, but the "exit" prevents the email from being sent

        return new \Symfony\Component\HttpFoundation\Response(
            '<script type="text/javascript">window.location = "'.$this->container->get('router')->generate('fos_user_resetting_check_email', array('email' => $this->getObfuscatedEmail($user))).'"</script>'
        );
    }
}

在我的layout.html.twig中,我更换了

{{ render(controller('FOSUserBundle:Resetting:sendEmail')) }}

通过

{{ render(controller('MyUserBundle:Resetting:sendEmail')) }}

我现在要在我的侧边栏中嵌入“check_email”操作,以及FOSUserBundle中的其他表单/模板。

答案 2 :(得分:0)

最简单的评论

    <requirement key="_method">POST</requirement>

    <!-- <requirement key="_method">POST</requirement> --> 

在/Resources/config/routing/resetting.xml?

由于传递的唯一信息是用户名(无论如何都是公开的)我希望&#34;我并没有把自己暴露在隐患中。

我有同样的问题,而且评论也有效(感谢用户的3409662评论)。