PUGXMultiUserBundle:自定义配置文件模板

时间:2014-03-05 16:33:23

标签: symfony pugxmultiuserbundle

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

更新:我认为我的捆绑需要对配置负责,所以我正在解密和应用CompilerPassInterface。

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

config.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())
        );
    }

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

某种进步。花了一段时间,但很明显我无法在PUGXMultiUserBundle配置中添加模板参数。所以我决定创建自己的配置。不得不确保对我自己的代码做一些其他类似hack的修复,但这至少看起来有效。 (我不接受我的回答;我宁愿让Patt的解决方案得到实施。)但这就是我所做的:

config.yml另外:

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

routing.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);
                }
            }
        }
...
}

的configuration.php

namespace Vol\UserBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

/**
 * 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);
    }

}