liip/theme-bundle

此包已弃用,不再维护。作者建议使用sylius/theme-bundle包。

为#Symfony2 Bundles提供主题支持

安装次数: 2,137,088

依赖者: 15

推荐者: 2

安全: 0

星星: 292

关注者: 43

分支: 76

开放性问题: 27

类型:symfony-bundle

1.7.0 2019-06-19 12:53 UTC

README

此项目不再维护,建议使用SyliusThemeBundle来支持Symfony 5和Twig 3。有关迁移,请参阅迁移到SyliusThemeBundle

此包为您提供将主题添加到每个包的可能性。在您的包目录中,它将在Resources/themes/<themename>下或回退到正常的Resources/views中查找,如果没有找到匹配的文件。

Build Status

安装

安装是一个快速(我保证!)的3步过程

  1. 下载LiipThemeBundle
  2. 启用包
  3. 导入LiipThemeBundle路由

第1步:使用composer安装LiipThemeBundle

运行以下composer require命令

$ php composer.phar require liip/theme-bundle

第2步:启用包

最后,在kernel中启用此包

<?php
// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new Liip\ThemeBundle\LiipThemeBundle(),
    );
}

第3步:导入LiipThemeBundle路由文件

现在您已激活并配置了包,剩下要做的就是导入LiipThemeBundle路由文件。

在YAML中

# app/config/routing.yml
liip_theme:
    resource: "@LiipThemeBundle/Resources/config/routing.xml"
    prefix: /theme

或者如果您喜欢XML

<!-- app/config/routing.xml -->
<import resource="@LiipThemeBundle/Resources/config/routing.xml" prefix="/theme" />

配置

您必须设置可能的主题和当前活动主题。必须确保活动主题是主题列表的一部分。

# app/config/config.yml
liip_theme:
    themes: ['standardTheme', 'winter_theme', 'weekend']
    active_theme: 'standardTheme'

针对不同设备的主题/模板

您可以为不同的设备(例如:桌面、平板电脑、手机、纯文本)提供特定的主题或模板。将选项autodetect_theme设置为true以根据用户代理设置current_device参数

# app/config/config.yml
liip_theme:
    autodetect_theme: true

然后在path_patterns中,您可以使用%%current_device%%参数(您的设备类型作为值)

# app/config/config.yml
liip_theme:
    path_patterns:
        app_resource:
            - %%app_path%%/themes/%%current_theme%%/%%current_device%%/%%template%%
            - %%app_path%%/themes/fallback_theme/%%current_device%%/%%template%%
            - %%app_path%%/views/%%current_device%%/%%template%%

可选地,autodetect_theme也可以设置为实现了Liip\ThemeBundle\Helper\DeviceDetectionInterface接口的DIC服务ID。

从cookie获取活动主题信息

如果您要根据cookie选择活动主题,可以添加

# app/config/config.yml
liip_theme:
    cookie:
        name: NameOfTheCookie
        lifetime: 31536000 # 1 year in seconds
        path: /
        domain: ~
        secure: false
        http_only: false

禁用基于控制器的主题切换

如果您的应用程序不允许用户切换主题,您可以禁用随包提供的控制器

# app/config/config.yml
liip_theme:
    load_controllers: false

主题层叠顺序

在检查位于包中的模板时,将应用以下顺序,例如,主题名称为 phone 的模板 @BundleName/Resources/template.html.twig 位于以下位置:

  1. 覆盖主题目录:app/Resources/themes/phone/BundleName/template.html.twig
  2. 覆盖视图目录:app/Resources/BundleName/views/template.html.twig
  3. 包主题目录:src/BundleName/Resources/themes/phone/template.html.twig
  4. 包视图目录:src/BundleName/Resources/views/template.html.twig

例如,如果您想集成有关主题架构的某些 TwigBundle 自定义错误页面,您必须使用以下目录结构:app/Resources/themes/phone/TwigBundle/Exception/error404.html.twig

在检查应用程序范围内的基本模板时,将应用以下顺序,例如,主题名称为 phone::template.html.twig 位于以下位置:

  1. 覆盖主题目录:app/Resources/themes/phone/template.html.twig
  2. 覆盖视图目录:app/Resources/views/template.html.twig

更改主题层叠顺序

您可以通过配置指令更改层叠顺序:path_patterns.app_resourcepath_patterns.bundle_resourcepath_patterns.bundle_resource_dir。例如:

# app/config/config.yml
liip_theme:
    path_patterns:
        app_resource:
            - %%app_path%%/themes/%%current_theme%%/%%template%%
            - %%app_path%%/themes/fallback_theme/%%template%%
            - %%app_path%%/views/%%template%%
        bundle_resource:
            - %%bundle_path%%/Resources/themes/%%current_theme%%_%%current_device%%/%%template%%
            - %%bundle_path%%/Resources/themes/%%current_theme%%/%%template%%
            - %%bundle_path%%/Resources/themes/fallback_theme/%%template%%
        bundle_resource_dir:
            - %%dir%%/themes/%%current_theme%%/%%bundle_name%%/%%template%%
            - %%dir%%/themes/fallback_theme/%%bundle_name%%/%%template%%
            - %%dir%%/%%bundle_name%%/%%override_path%%
层叠顺序模式占位符
占位符 表示 示例
%app_path% 应用程序资源所在的路径 app/Resources
%bundle_path% 包所在的路径,例如 src/Vendor/CoolBundle/VendorCoolBundle
%bundle_name% 包的名称 VendorCoolBundle
%dir% 资源应首先查找的目录
%current_theme% 当前活动主题的名称
%current_device% 当前设备类型的名称 desktop、phone、tablet、plain
%template% 模板名称 view.html.twig
%override_path% 类似于模板,但使用视图目录 views/list.html.twig

更改活动主题

为此,请参阅 ThemeRequestListener。

如果您处于请求周期的早期,并且尚未渲染任何模板,您仍然可以更改主题而不会出现问题。为此,主题服务位于以下位置:

$activeTheme = $container->get('liip_theme.active_theme');
echo $activeTheme->getName();
$activeTheme->setName("phone");

主题特定控制器

在某些情况下,不同的模板不足以满足需求,您需要为特定主题使用不同的控制器。我们在 A/B 测试中遇到了这种情况。不要滥用此功能,并检查您的用例是否还应被视为主题。

默认情况下,此功能不激活,因为涉及额外的请求监听器。通过在配置中设置 theme_specific_controllers 来启用它

# app/config/config.yml
liip_theme:
    # ...
    theme_specific_controllers: true

现在您可以在路由文件中为每个主题配置控制器

my_route:
    path: /x/y
    defaults:
        _controller: my_service:fooAction
        theme_controllers:
            a: my_other_service:fooAction
            b: App:Other:foo

像往常一样,您可以使用服务表示法或命名空间表示法为控制器。只需在 theme_controllers 键下指定主题下的控制器即可。

Assetic 集成

由于 LiipThemeBundle 覆盖了模板定位器服务的方式,assetic 只会转储活动主题的资产。

为了转储所有主题的资产,启用 assetic_integration 选项

# app/config/config.yml
liip_theme:
    # ...
    assetic_integration: true

这将覆盖 Twig 公式加载器,并遍历所有主题,确保所有资产都被转储。

请注意,这仅适用于 AsseticBundle 2.1 或更高版本。

覆盖设备检测

可以覆盖用于设备检测的服务。请确保扩展 DeviceDetection 或实现 DeviceDetectionInterface

# app/config/config.yml
services:
    my_devcice_detection:
        class: SomeClass

liip_theme:
    # ...
    device_detection: my_devcice_detection

迁移到 SyliusThemeBundle

这将向您展示如何从 LiipThemeBundle 切换到 SyliusThemeBundle

删除旧的主题包并安装 SyliusThemeBundle

# Remove old theme-bundle
composer remove liip/theme-bundle --no-update

# Install new theme-bundle
composer require sylius/theme-bundle:"^2.0"

删除旧配置

需要删除旧的 liip_theme.yaml 配置

-liip_theme:
-    themes: ['awesome']
-    active_theme: 'awesome'

在下一步中,您将看到如何使用 SyliusThemeBundle 配置 出色的 主题。

配置 SyliusThemeBundle

为了使用该包,您必须添加以下默认配置

# ./config/packages/sylius_theme.yaml

sylius_theme:
    sources:
        filesystem: ~

默认情况下,该包在 %kernel.project_dir%/themes 目录中查找主题,并寻找名为 composer.json 的配置文件。这可以通过 yaml 配置进行更改

sylius_theme:
    sources:
        filesystem:
            filename: theme.json

转换主题配置

在 SyliusThemeBundle 中,每个主题都必须有一个自己的配置文件,形式为 theme.json。添加一个 theme.json 文件,并添加以下最小配置

{
    "name": "app/awesome"
}

有关配置选项的详细文档,请参阅 主题配置参考

您可能需要更改主题名称。重要的是,name 与 composer 的命名约定(vendor/name)相匹配。此外,theme.json 必须移动到特定主题的目录中。

例如: %kernel.project_dir%/themes/awesome/theme.json

更新项目结构

您的模板必须放置在 theme.json 文件旁边的 templates 目录中。

例如: %kernel.project_dir%/themes/<theme-name>/templates

这导致以下项目结构

ProjectName
├── composer.json
├── assets
├── bin
├── config
├── templates
├── themes
│   ├── awesome
│   │   ├── templates
│   │   │   └── base.html.twig
│   │   └── theme.json
│   └── <theme-name-2>
│       ├── templates
│       │   └── base.html.twig
│       └── theme.json
├── ...
└── ...

如项目结构所示,每个主题必须在模板目录旁边有自己的 theme.json 配置文件。

创建 ThemeRequestListener

您需要创建一个 ThemeRequestListener 来根据当前 $request 数据设置主题

use Sylius\Bundle\ThemeBundle\Context\SettableThemeContext;
use Sylius\Bundle\ThemeBundle\Repository\ThemeRepositoryInterface;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;

final class ThemeRequestListener
{
    /** @var ThemeRepositoryInterface */
    private $themeRepository;

    /** @var SettableThemeContext */
    private $themeContext;

    public function __construct(ThemeRepositoryInterface $themeRepository, SettableThemeContext $themeContext)
    {
        $this->themeRepository = $themeRepository;
        $this->themeContext = $themeContext;
    }

    public function onKernelRequest(GetResponseEvent $event): void
    {
        if (HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) {
            // don't do anything if it's not the master request
            return;
        }

        $themeName = 'app/awesome';

        // here you can set the $themeName based on $event->getRequest() object

        $this->themeContext->setTheme(
            $this->themeRepository->findOneByName($themeName)
        );
    }
}

还可以查看 SyliusThemeBundle 文档

贡献

欢迎积极贡献和补丁。为了保持事物整洁,我们有很多单元测试。如果您正在提交拉取请求,请确保它们仍在通过,如果您添加了功能,请查看覆盖率,它应该相当高 :)

首先安装依赖项

   composer.phar install --dev

这将给出正确的结果

phpunit --coverage-text