PUGXMultiUserBundle:自定义配置文件模板



启用自定义用户配置文件模板的方法涉及修改供应商的Configuration.php。 有没有一种方法没有? 或者这是一个可行的解决方案?当前方法如下所示:

更新:我认为我的捆绑包需要负责配置,所以我正处于破译和应用 CompilerPassInterface 的阵痛中。

[编辑:提问的另一种方式 - 这可以通过在模板选项前面加上PrependExtensionInterface来完成吗? 如果是这样,那怎么可能呢?

配置.yml

pugx_multi_user:
  users:
    staff:
        entity: 
          class: AcmeMyBundleEntityStaff
#          factory: 
        registration:
          form: 
            type: AcmeUserBundleFormRegistrationStaffFormType
            name: fos_user_registration_form
            validation_groups:  [Registration, Default]
          template: AcmeUserBundle:Registration:staff.form.html.twig
        profile:
          form:
            type: AcmeUserBundleFormProfileStaffFormType
            name: fos_user_profile_form
            validation_groups:  [Profile, Default]
# template line added
          template: AcmeUserBundle:Profile:staff.form.html.twig 
         ...

摘自PUGXMultiUserBundleDependencyInjectionConfiguration.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()
...

配置文件控制器(在扩展用户捆绑包中)

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 SymfonyComponentEventDispatcherEventDispatcherInterface */
        $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 FOSUserBundleFormFactoryFactoryInterface */
        $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 FOSUserBundleModelUserManagerInterface */
                $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())
        );
    }

我在这里创建了一个处理自定义配置文件模板的拉取请求。

某种进度。 这花了一段时间,但很明显我无法将模板参数添加到 PUGXMultiUserBundle 配置中。 所以我决定创建自己的配置。 必须确保对我自己的代码进行其他一些类似黑客的修复,但这至少看起来有效。 (我不会接受我的回答;我宁愿帕特的解决方案得到实现。 但这是我所做的:

配置.yml 添加:

vol_user:
    staff: VolUserBundle:Profile:staff.form.html.twig
    volunteer: VolUserBundle:Profile:volunteer.form.html.twig
    admin: VolUserBundle:Profile:admin.form.html.twig

路由.yml 更改为 fos_user_profile(_edit添加到参数名称):

fos_user_profile_edit:
    resource: "@FOSUserBundle/Resources/config/routing/profile.xml"
    prefix: /profile

修订后的控制器:

    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);
        $templates = $this->container->getParameter('vol_user');
        foreach ($users as $userType) {
            if ($userType['entity']['class'] == $class) {
                $l = strrpos($class, DIRECTORY_SEPARATOR ) + 1;
                $type = strtolower(substr($class, $l));
                $templateString = $templates[$type];
                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);
                }
            }
        }
...
}

配置.php

namespace VolUserBundleDependencyInjection;
use SymfonyComponentConfigDefinitionBuilderTreeBuilder;
use SymfonyComponentConfigDefinitionConfigurationInterface;
/**
 * This is the class that validates and merges configuration from your app/config files
 *
 * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html#cookbook-bundles-extension-config-class}
 */
class Configuration implements ConfigurationInterface
{
    /**
     * {@inheritDoc}
     */
    public function getConfigTreeBuilder()
    {
        $treeBuilder = new TreeBuilder();
        $rootNode = $treeBuilder->root('vol_user');
        $rootNode->
                children()
                    ->scalarNode('staff')->defaultValue(null)->end()
                    ->scalarNode('volunteer')->defaultValue(null)->end()
                    ->scalarNode('admin')->defaultValue(null)->end()
                ->end()
                ->end();
        return $treeBuilder;
    }
}

VolUserExtension

class VolUserExtension extends Extension
{
    /**
     * {@inheritDoc}
     */
    public function load(array $configs, ContainerBuilder $container)
    {
        $configuration = new Configuration();
        $config = $this->processConfiguration($configuration, $configs);
        $container->setParameter('vol_user', $config);
    }
}

相关内容

  • 没有找到相关文章

最新更新