2

FOSUserBundle の change_password フォームを、change_email フォームまたはその他の情報もある設定ページに統合したいと考えています。FOS を設計に統合する方法は知っていますが、やろうとしていることを実行する方法は知りません。

現在、フォームはコントローラーとフォームビルダーメソッドを介して生成されていますが、これを変更する方法がわかりません...

前もってありがとう、バレンティン

4

1 に答える 1

7

(これは8か月前の質問ですが、誰かの助けになるかもしれません。)

最も簡単な方法は、ユーザー プロファイル用に 1 ページのみを作成する場合、パスワード変更イベント用に別のフォームを使用することです。FOSUserBundle はこの機能を提供します。したがって、独自のルートとフォームを使用する場合は、FOS コントローラーとフォームからコードをコピーし、いくつかのパラメーター (ルート名やデザインなど) を変更するだけで準備完了です。このバンドルを使用するには、おそらくもっと洗練された方法がいくつかありますが、私の意見では、これが最も簡単で柔軟です。

FOSUserBundle は/vendor/friendsofsymfony/user-bundle/FOS/UserBundle/ディレクトリにあります。パスワード コントローラは に/Controller/ChangePasswordController.php あり、フォームは にあり/Resources/views/ChangePasswordます。

設定ページでこれを行った方法は次のとおりです。私はパスワード変更機能のみを使用しますが、さまざまなフォームに対して個別のアクションを実行し、ビューを使用して元のインデックス ページにユーザーをリダイレクトできると思います。

これはコントローラーです(リダイレクトルートとビューのみを変更しました):

use JMS\SecurityExtraBundle\Annotation\Secure;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use FOS\UserBundle\Model\UserInterface;

class SettingsController extends Controller
{
    /**
    * @Secure(roles="ROLE_USER")
    */
    public function indexAction()
    {
        $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.change_password.form');
        $formHandler = $this->container->get('fos_user.change_password.form.handler');

        $process = $formHandler->process($user);
        if ($process) {
            $this->get('session')->setFlash('notice', 'Password changed succesfully');

            return $this->redirect($this->generateUrl('settings'));
        }

        return $this->render('AcmeHelloBundle:Settings:password.html.twig', ['form' => $form->createView()]);
    }
}

これがビュー (password.html.twig) です - ここでの唯一の変更はルートです: path('settings')

<form action="{{ path('settings') }}" {{ form_enctype(form) }} method="POST" class="fos_user_change_password">
    {{ form_widget(form) }}
    <div>
        <input type="submit" value="{{ 'change_password.submit'|trans({}, 'FOSUserBundle') }}" />
    </div>
</form>

それでこれです。これで、素敵なパスワード変更フォームができました。面倒な作業はすべて FOS UserBundle によって処理されます!

于 2013-06-07T10:23:41.283 に答える