可能为时已晚,但它可以帮助其他人
我在使用 Symfony 和 FOSUserBundle 时遇到了同样的问题。
在 FOSUserBundle 中,配置文件链接是:
site_url/profile/
site_url/profile/编辑
site_url/profile/更改密码
我有一个管理和仪表板面板,我希望链接在管理面板中
site_url/admin/profile/
site_url/admin/profile/edit
site_url/admin/profile/更改密码
在用户仪表板中
site_url/dashboard/profile/
site_url/dashboard/profile/edit
site_url/dashboard/profile/更改密码
要获得这种行为,无需 EventListener 和 TwigExtesion
非常简单
这里的解决方案:
替换
fos_user:
resource: "@FOSUserBundle/Resources/config/routing/all.xml"
通过
fos_user_security:
resource: "@FOSUserBundle/Resources/config/routing/security.xml"
fos_user_registration:
resource: "@FOSUserBundle/Resources/config/routing/registration.xml"
fos_user_resetting:
resource: "@FOSUserBundle/Resources/config/routing/resetting.xml"
fos_user_change_password:
resource: "@FOSUserBundle/Resources/config/routing/change_password.xml"
prefix: /{prefix}/profile
fos_user_profile:
resource: "@FOSUserBundle/Resources/config/routing/profile.xml"
prefix: /{prefix}/profile
在模板中使用:
<a href="{{ path('fos_user_profile_show', {'prefix': 'dashboard'}) }}">Show Profile</a>
<a href="{{ path('fos_user_profile_edit', {'prefix': 'dashboard'}) }}">Edit Profile</a>
<a href="{{ path('fos_user_change_password', {'prefix': 'dashboard'}) }}">Change Password</a>
或者你想要的任何其他前缀
现在,如果您使用例如更改密码链接,您将收到此错误
在渲染模板期间引发了异常(“一些
缺少强制参数(“前缀”)来生成 URL
路由“fos_user_change_password”。”)在
FOSUserBundle:ChangePassword:changePassword_content.html.twig 在行
3.
您可以像这样在模板中添加前缀:
替换
<form action="{{ path('fos_user_change_password') }}" {{ form_enctype(form) }} method="POST" class="fos_user_change_password">
通过
<form action="{{ path('fos_user_change_password', {'prefix': app.request.attributes.get('prefix') }) }}" {{ form_enctype(form) }} method="POST" class="fos_user_change_password">
这会给你这个错误
缺少一些强制参数(“前缀”)来生成 URL
路由“fos_user_profile_show”。
我找到的最终解决方案是覆盖控制器
只需将洞控制复制到您的应用控制器文件夹中,更改命名空间并替换
if (null === $response = $event->getResponse()) {
$url = $this->generateUrl('fos_user_profile_show');
$response = new RedirectResponse($url);
}
通过
if (null === $response = $event->getResponse()) {
$url = $this->generateUrl('fos_user_profile_show',array('prefix'=>$request->get('prefix')));
$response = new RedirectResponse($url);
}
在您不需要该操作的形式中,它将是
<form {{ form_enctype(form) }} method="POST" class="fos_user_change_password">