启用自定义用户配置文件模板的方法涉及修改供应商的Configuration.php
. 有没有没有的方法?或者这是一个可行的解决方案?当前方法如下所示:
更新:我认为我的包需要对配置负责,所以我正处于破译和应用 CompilerPassInterface 的阵痛中。
[编辑:提出问题的另一种方式 - 这可以通过在模板选项前面加上PrependExtensionInterface
?如果是这样,那将如何运作?]
配置.yml
pugx_multi_user:
users:
staff:
entity:
class: Acme\MyBundle\Entity\Staff
# factory:
registration:
form:
type: Acme\UserBundle\Form\RegistrationStaffFormType
name: fos_user_registration_form
validation_groups: [Registration, Default]
template: AcmeUserBundle:Registration:staff.form.html.twig
profile:
form:
type: Acme\UserBundle\Form\ProfileStaffFormType
name: fos_user_profile_form
validation_groups: [Profile, Default]
# template line added
template: AcmeUserBundle:Profile:staff.form.html.twig
...
摘自PUGX\MultiUserBundle\DependencyInjection\Configuration.php
[注意添加->scalarNode('template')->defaultValue(null)->end()
]
...
->children()
->arrayNode('profile')
->addDefaultsIfNotSet()
->children()
->arrayNode('form')
->addDefaultsIfNotSet()
->children()
->scalarNode('type')->defaultValue(null)->end()
->scalarNode('name')->defaultValue('fos_user_profile_form')->end()
->arrayNode('validation_groups')
->prototype('scalar')->end()
->defaultValue(array('Profile', 'Default'))
->end()
->end()
->end()
->scalarNode('template')->defaultValue(null)->end()
->end()
->end()
->end()
...
ProfileController(在扩展的用户包中)
class ProfileController extends BaseController
{
/**
* Edit the user
*/
public function editAction(Request $request)
{
$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.');
}
$discriminator = $this->container->get('pugx_user.manager.user_discriminator');
$users = $this->container->getParameter('pugx_user_discriminator_users');
$class = $discriminator->getClass($user);
foreach ($users as $userType) {
if ($userType['entity']['class'] == $class) {
$templateString = $userType['profile']['template'];
if (false === strpos($templateString, $this->container->getParameter('fos_user.template.engine'))) {
$template = 'FOSUserBundle:Profile:edit.html.';
} else {
$l = strrpos($templateString, ".");
$template = substr($templateString, 0, $l + 1);
}
}
}
/** @var $dispatcher \Symfony\Component\EventDispatcher\EventDispatcherInterface */
$dispatcher = $this->container->get('event_dispatcher');
$event = new GetResponseUserEvent($user, $request);
$dispatcher->dispatch(FOSUserEvents::PROFILE_EDIT_INITIALIZE, $event);
if (null !== $event->getResponse()) {
return $event->getResponse();
}
/** @var $formFactory \FOS\UserBundle\Form\Factory\FactoryInterface */
$formFactory = $this->container->get('fos_user.profile.form.factory');
$form = $formFactory->createForm();
$form->setData($user);
if ('POST' === $request->getMethod()) {
$form->bind($request);
if ($form->isValid()) {
/** @var $userManager \FOS\UserBundle\Model\UserManagerInterface */
$userManager = $this->container->get('fos_user.user_manager');
$event = new FormEvent($form, $request);
$dispatcher->dispatch(FOSUserEvents::PROFILE_EDIT_SUCCESS, $event);
$userManager->updateUser($user);
if (null === $response = $event->getResponse()) {
$url = $this->container->get('router')->generate('fos_user_profile_show');
$response = new RedirectResponse($url);
}
$dispatcher->dispatch(FOSUserEvents::PROFILE_EDIT_COMPLETED, new FilterUserResponseEvent($user, $request, $response));
return $response;
}
}
return $this->container->get('templating')->renderResponse(
$template . $this->container->getParameter('fos_user.template.engine'), array('form' => $form->createView())
);
}