我想將FOSUserBundle的change_password表單集成到我的設置頁面,其中還有change_email表單或其他信息。我知道如何在我的設計中集成FOS,但不知道如何去做我想做的事情。現在FOSUserBundle將更改密碼錶單與其他表格集成
,形式產生通過控制器和一個表單生成方法,我不知道如何修改這個...提前
感謝, 瓦倫丁
我想將FOSUserBundle的change_password表單集成到我的設置頁面,其中還有change_email表單或其他信息。我知道如何在我的設計中集成FOS,但不知道如何去做我想做的事情。現在FOSUserBundle將更改密碼錶單與其他表格集成
,形式產生通過控制器和一個表單生成方法,我不知道如何修改這個...提前
感謝, 瓦倫丁
(我看到這是一個8個月的問題,但也許它可以幫助某人。)
最簡單的方法是對密碼更改事件使用單獨的表單,如果您只想爲用戶配置文件製作一個頁面。 FOSUserBundle提供此功能。所以如果你想使用你自己的路線和表格,你可以複製FOS控制器和表格中的代碼,改變一些參數(比如路線名稱和設計),然後設置好。可能有一些更復雜的方法來使用這個Bundle,但在我看來這是最簡單和最靈活的。
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) - 這裏唯一的變化是路徑:路徑( '設置')
<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來完成!