eth8505/zend-mvc-themer

此包已弃用且不再维护。作者建议使用eth8505/laminas-mvc-themer包。

为zend-mvc应用添加主题支持的模块

1.0.0 2018-03-12 21:42 UTC

This package is auto-updated.

Last update: 2020-09-11 10:04:19 UTC


README

!!!重要!!! 此包不再维护,因为Zendframework已重命名为laminas。请使用eth85055/laminas-mvc-themer。

Eth8585\ZendMvcThemer模块为Zend\Mvc应用添加主题支持。

如何安装

通过composer安装eth8505/zend-mvc-themer包。

$ composer require eth8505/zend-mvc-themer

在您的application.config.php文件中加载模块,如下所示

<?php

return [
	'modules' => [
		'Eth8585\ZendMvcThemer\\',
		// ...
	],
];

如何使用

1 创建主题

如上所述,主题需要通过主题插件管理器进行注册。一个主题必须实现ThemeInterface。除getName之外的所有方法都可能返回一个空数组。请检查DefaultTheme类以获取仅指定名称的主题的空实现。

1.1 指定自定义样式表

在您的主题中实现getStylesheet方法,并返回一个数组。请注意,所有路径都将通过BasePath视图助手传递,因此必须相对于您的public/目录。

<?php

use Eth8505\ZendMvcThemer\Theme\ThemeInterface;

class MyTheme implements ThemeInterface {
    // ...
    public function getStylesheets() : array {
        return [
            'css/theme/my/custom/file.css'
        ];
    }
    // ...
}

所有样式表都使用HeadLink视图助手的appendStylesheet方法注入。

1.2 指定自定义JavaScript

在您的主题中实现getScripts方法,并返回一个数组。请注意,所有路径都将通过BasePath视图助手传递,因此必须相对于您的public/目录。

<?php

use Eth8505\ZendMvcThemer\Theme\ThemeInterface;

class MyTheme implements ThemeInterface {
    // ...
    public function getScripts() : array {
        return [
            'css/theme/my/custom/file.js'
        ];
    }
    // ...
}

所有脚本都使用HeadScript视图助手的prependFile方法注入。

1.3 指定自定义变量

在您的主题中实现getVariables方法,并返回一个数组。请注意,所有路径都将通过BasePath视图助手传递,因此必须相对于您的public/目录。

<?php

use Eth8505\ZendMvcThemer\Theme\ThemeInterface;

class MyTheme implements ThemeInterface {
    // ...
    public function getVariables() : array {
        return [
            'heading1' => 'one',
            'heading2' => [
                'key1' => 'test'
            ]
        ];
    }
    // ...
}

主题变量不会自动注入到您的视图模型中,因为这可能会干扰您在视图模型中设置的任何内容。然而,该模块提供了一个theme()视图助手,允许访问主题变量。

<html>
    <body>
        <h1><?= $this->theme()->var('heading1') ?></h1>
        <h2><?= $this->theme()->var('heading2/key1') ?></h2>
    </body>
</html>

1.4 指定自定义元标签

元标签比脚本、样式或变量更复杂,因为有两种基本类型:namehttp-equiv。使用此模块,我们为两者使用相同的语法,在定义数组中指定type作为键。要实现自定义元标签,在您的主题中实现getMetaTags方法。

<?php

use Eth8505\ZendMvcThemer\Theme\ThemeInterface;

class MyTheme implements ThemeInterface {
    // ...
    public function getMetaTags() : array {
        return [
            [
                'type' => 'name',
                'name' => 'robots',
                'content' => 'noindex,nofollow'
            ],
            [
                'type' => 'http-equiv',
                'name' => 'refresh',
                'content' => '30'
            ]
        ];
    }
    // ...
}

2. 在服务管理器中注册

您可以通过您的module.config.php中的配置将您的主题注册到服务管理器中

<?php

return [
    'zend-mvc-themes' => [
        'invokables' => [
            MyTheme::class
        ]
    ]
];

或者在您的模块类中使用ThemeProviderInterface注册它

<?php

namespace MyModule;

use Eth8505\ZendMvcThemer\Theme\ThemeProviderInterface;

class Module implements ThemeProviderInterface {
    
    /**
     * @inheritdoc 
     */
    public function getThemeConfig() {

        return [
            'invokables' => [
                MyTheme::class
            ]
        ];
        
    }
    
}

3. 解决主题

默认情况下,主题解决是通过使用ConfigResolver类完成的,该类简单地检查配置zend-mvc-themer/resolver,并将主题作为theme服务注入。

3.1 自定义主题解析器

除了默认基于配置的主题解析器之外,您还可以指定一个自定义解析器类。这可以是您选择的任何实现ThemeResolverInterface的类,从会话中读取主题(如果您想为用户提供主题选择)。

基于主机名的简单主题解析器示例实现

<?php
namespace MyModule;

use Eth8505\ZendMvcThemer\Resolver\ThemeResolverInterface;
use Eth8505\ZendMvcThemer\Theme\ThemeInterface;
use Eth8505\ZendMvcThemer\Theme\ThemePluginManager;
use Zend\Http\Request;

class Module implements ThemeResolverInterface {

    private $request;
    
    private $pluginManager;

    public function __construct(Request $request, ThemePluginManager $pluginManager) {
        $this->request = $request;
        $this->pluginManager = $pluginManager;
    }

    public function resolve() : ThemeInterface  {
        
        if ($this->request->getUri()->getHost() === 'my.awesome.host') {
            $theme = ThemeOne::class;
        } else {
            $theme = ThemeTwo::class;
        }
        
        return $this->pluginManager->get($theme);
        
    }

}