Val*_*inH 1 php symfony fosuserbundle
我想在我的设置页面中集成FOSUserBundle的change_password表单,我还有change_email表单或其他信息.我知道如何在我的设计中集成FOS而不是如何做我正在尝试做的事情.
现在,表单是通过控制器和表单生成器方法生成的,我不知道如何修改它...
在此先感谢,Valentin
(我看到这是一个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()]);
}
}
Run Code Online (Sandbox Code Playgroud)
这是视图(password.html.twig) - 这里唯一的变化是路径:路径('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>
Run Code Online (Sandbox Code Playgroud)
所以就是这样.现在你有一个很好的密码更改表格,所有繁重的工作都由FOS UserBundle照顾!