Symfony2细枝主题从数据库?

Symfony2细枝主题从数据库?,symfony,twig,Symfony,Twig,如何使用Symfony2添加对主题的支持,其中每个用户的主题(当前主题的路径)存储在数据库中?例如: ------------------------------------------------------------- | User | id | username | password | theme_name | ------------------------------------------------------------- Bob

如何使用Symfony2添加对主题的支持,其中每个用户的主题(当前主题的路径)存储在数据库中?例如:

-------------------------------------------------------------
| User              | id | username | password | theme_name |
-------------------------------------------------------------
  Bob                 1    Bob        327n829    /Default
  Alice               2    Alice      2c839n42   /Pink
因此,对于给定的用户,Symfony必须从
Resources/views/{theme\u name}
加载正确的模板,如果模板不存在,则返回到
Resources/views/Default

我已经检查了这两个包:

  • 从字符串加载细枝模板的
  • 它允许在YAML配置文件中定义主题

但两者似乎都不符合我的需要。非常感谢您的帮助。

我们使用LiipThemeBundle和内核侦听器:

public function onEarlyKernelRequest(GetResponseEvent $event)
{
    $request = $event->getRequest();

    if ($request->attributes->has('_theme') === false) {
        // if you use annotation with doctrine, you'll need to register them before that line
        $user = $this->container->get('doctrine.orm.entity_manager')->getRepository('UserBundle:User')->findBy(...);

       // you probably will want to set _theme in request context, not in the $request object
        $request->attributes->set('_theme', $user->getTheme());
        $this->container->get('liip_theme.active_theme')->setName($user->getTheme());
    }
    //$this->router->setContext($context);
}

您需要考虑LiipThemeBundle的一些特性。

我们使用LiipThemeBundle和内核侦听器:

public function onEarlyKernelRequest(GetResponseEvent $event)
{
    $request = $event->getRequest();

    if ($request->attributes->has('_theme') === false) {
        // if you use annotation with doctrine, you'll need to register them before that line
        $user = $this->container->get('doctrine.orm.entity_manager')->getRepository('UserBundle:User')->findBy(...);

       // you probably will want to set _theme in request context, not in the $request object
        $request->attributes->set('_theme', $user->getTheme());
        $this->container->get('liip_theme.active_theme')->setName($user->getTheme());
    }
    //$this->router->setContext($context);
}

您需要考虑LiipThemeBundle的一些特性。

谢谢您的回答,但我真的不需要仅为了更改路径而使用捆绑包!我可以问你为什么要在RouteListener中更改活动主题吗?编辑:不需要切换传递主题的主题参数。@Gremo LiipThemeBundle是将主题支持添加到项目中的一种方法。也就是说,“Symfony必须从Resources/views/{theme_name}加载正确的模板,如果模板不存在,则返回到Resources/views/Default。”。这不是例行的倾听者,而是最早的倾听者。“这不需要切换传递主题的主题参数”你是什么意思?@Gremo-你有没有这样做过?我也有同样的问题?谢谢你的回答,但我真的不需要仅仅为了改变路径而使用捆绑包!我可以问你为什么要在RouteListener中更改活动主题吗?编辑:不需要切换传递主题的主题参数。@Gremo LiipThemeBundle是将主题支持添加到项目中的一种方法。也就是说,“Symfony必须从Resources/views/{theme_name}加载正确的模板,如果模板不存在,则返回到Resources/views/Default。”。这不是例行的倾听者,而是最早的倾听者。“这不需要切换传递主题参数的主题”你是什么意思?@Gremo-你有没有试过我也有同样的问题?