Zend framework2 如何在Zend Framework 2应用程序中处理元描述/关键字?

Zend framework2 如何在Zend Framework 2应用程序中处理元描述/关键字?,zend-framework2,keyword,meta-tags,head,Zend Framework2,Keyword,Meta Tags,Head,布局文件中定义的默认页面标题可以扩展(PREPEND/APPEND)或替换(SET)为当前视图指定的标题 这种行为我也一直期待视图助手。现在我找不到任何合适的方法来“扩展”布局文件中定义的默认描述/关键字 我可以用append(…)/appendName(…)(类似于用或添加样式或脚本文件)来替代它。但是它对于描述和关键字元标记没有意义 有没有推荐的方法来处理这个问题?如何处理描述/关键字meta标记?以下是我目前正在使用的一个快速而肮脏的解决方案(为了获得干净的解决方案,而不是丑陋的解决方案,

布局文件中定义的默认页面标题可以扩展(
PREPEND
/
APPEND
)或替换(
SET
)为当前视图指定的标题

这种行为我也一直期待视图助手。现在我找不到任何合适的方法来“扩展”布局文件中定义的默认
描述
/
关键字

我可以用
append(…)
/
appendName(…)
(类似于用或添加样式或脚本文件)来替代它。但是它对于
描述
关键字
元标记没有意义


有没有推荐的方法来处理这个问题?如何处理
描述
/
关键字
meta
标记?

以下是我目前正在使用的一个快速而肮脏的解决方案(为了获得干净的解决方案,而不是丑陋的解决方案,需要在框架上进行更改):

头元视图辅助对象

<?php
namespace MyNamespace\View\Helper;

use stdClass;
use Zend\View;
use Zend\View\Exception;
use Zend\View\Helper\HeadMeta as ZendHeadMeta;
use Zend\View\Helper\Placeholder\Container\AbstractContainer;

class HeadMeta extends ZendHeadMeta {

    const DELIMITER = ' - ';

    /**
     * @see \Zend\View\Helper\HeadMeta::__invoke()
     */
    public function __invoke($content = null, $keyValue = null, $keyType = 'name', $modifiers = array(), $placement = AbstractContainer::APPEND) {
        parent::__invoke($content, $keyValue, $keyType, $modifiers, $placement);
        return $this;
    }

    /**
     * @see \Zend\View\Helper\HeadMeta::append()
     */
    public function append($value) {
        if ($value->name == 'description') {
            $this->updateDescription($value, AbstractContainer::APPEND);
        } else if ($value->name == 'keywords') {
            $this->updateKeywords($value, AbstractContainer::APPEND);
        } else {
            parent::append($value);
        }
    }

    /**
     * @see \Zend\View\Helper\HeadMeta::prepend()
     */
    public function prepend($value) {
        if ($value->name == 'description') {
            $this->updateDescription($value, AbstractContainer::PREPEND);
        } else if ($value->name == 'keywords') {
            $this->updateKeywords($value, AbstractContainer::PREPEND);
        } else {
            parent::prepend($value);
        }
    }

    // Not working correctly!
    // Can cause a
    // Fatal error: Maximum function nesting level of '100' reached, aborting!
    /**
     * @see \Zend\View\Helper\HeadMeta::set()
     */
    public function set($value) {
        if ($value->name == 'description') {
            $this->updateDescription($value, AbstractContainer::SET);
        } else if ($value->name == 'keywords') {
            $this->updateKeywords($value, AbstractContainer::SET);
        } else {
            parent::set($value);
        }
    }

    /**
     * If a description meta already exsists, extends it with $value->content,
     * else creates a new desctiprion meta.
     * @param \stdClass $value
     * @param string $position
     */
    public function updateDescription(\stdClass $value, $position = AbstractContainer::SET) {
        $descriptionExists = false;
        foreach ($this->getContainer() as $item) {
            if ($this->isDescription($item)) {
                switch ($position) {
                    case AbstractContainer::APPEND:
                        $descriptionString = implode(static::DELIMITER, array($item->content, $value->content));
                        break;
                    case AbstractContainer::PREPEND:
                        $descriptionString = implode(static::DELIMITER, array($value->content, $item->content));
                        break;
                    case AbstractContainer::SET:
                    default:
                        $descriptionString = $value->content;
                        break;
                }
                $item->content = $descriptionString;
                $descriptionExists = true;
            }
        }
        if (!$descriptionExists) {
            switch ($position) {
                case AbstractContainer::APPEND:
                    parent::append($value);
                    break;
                case AbstractContainer::PREPEND:
                    parent::prepend($value);
                    break;
                case AbstractContainer::SET:
                default:
                    parent::set($value);
                    break;
            }
        }
    }

    /**
     * If a keywords meta already exsists, extends it with $value->content,
     * else creates a new keywords meta.
     * @param \stdClass $value
     * @param string $position
     */
    public function updateKeywords(\stdClass $value, $position = AbstractContainer::SET) {
        $keywordsExists = false;
        foreach ($this->getContainer() as $item) {
            if ($this->isKeywords($item)) {
                switch ($position) {
                        case AbstractContainer::APPEND:
                            $keywordsString = implode(', ', array($item->content, $value->content));
                            break;
                        case AbstractContainer::PREPEND:
                            $keywordsString = implode(', ', array($value->content, $item->content));
                            break;
                        case AbstractContainer::SET:
                        default:
                            $keywordsString = $value->content;
                            break;
                }
                $item->content = $keywordsString;
                $keywordsExists = true;
            }
        }
        if (!$keywordsExists) {
            parent::append($value);
        }
    }

    /**
     * @return description meta text
     */
    public function getDescription() {
        $description = null;
        foreach ($this->getContainer() as $item) {
            if ($this->isKeywords($item)) {
                $description = $item->content;
                break;
            }
        }
        return $description;
    }

    /**
     * @return keywords meta text
     */
    public function getKeywords() {
        $keywords = null;
        foreach ($this->getContainer() as $item) {
            if ($this->isKeywords($item)) {
                $keywords = $item->content;
                break;
            }
        }
        return $keywords;
    }

    /**
     * Checks, whether the input $item is an approproate object for $this->container
     * and wheter it's a description object (its name is "description")
     * @param stdClass $item
     * @throws Exception\InvalidArgumentException
     * @return boolean
     */
    public function isDescription(stdClass $item) {
        if (!in_array($item->type, $this->typeKeys)) {
            throw new Exception\InvalidArgumentException(sprintf(
                'Invalid type "%s" provided for meta',
                $item->type
            ));
        }
        return $item->name == 'description';
    }

    /**
     * Checks, whether the input $item is an approproate object for $this->container
     * and wheter it's a keywords object (its name is "keywords")
     * @param stdClass $item
     * @throws Exception\InvalidArgumentException
     * @return boolean
     */
    public function isKeywords(stdClass $item) {
        if (!in_array($item->type, $this->typeKeys)) {
            throw new Exception\InvalidArgumentException(sprintf(
                'Invalid type "%s" provided for meta',
                $item->type
            ));
        }
        return $item->name == 'keywords';
    }

}

链接到我的答案:

这是对我有用的东西。在布局文件中,您必须确保元标记被回显。在阶段中它是空的,但是您将标记输出元标记的位置。这种方法的唯一缺点是,似乎没有一种方法可以拥有默认的元标记,因此您必须在每个视图文件中添加元标记

布局文件中的

<?php echo $this->headMeta(); ?>
$this->headMeta("test description text", "description");

问题是如何替换先前定义的元标记。在您的示例中,前面定义的元不存在,所以您给出了不相关的答案。您的解决方案很有效,但请添加一点注释,即应该将助手正确地附加到模块中。例如,module.config.php
'view\u helpers'=>array('invokables'=>array('headMeta'=>'\YourModule\view\Helper\headMeta')中应该有以下行。
开始工作了!首先我添加了@zeliboba注释(带一个结束括号;)但随后出现错误:PHP致命错误:未捕获异常“Zend\\ServiceManager\\exception\\ServiceNotFoundException”,消息为“Zend\\View\\HelperPluginManager::createFromInvokable:通过可调用类“MyModule\\View\\Helper\\headmeta”检索“headmeta(别名:headmeta)”失败“;类不存在”-通过使用./bin/classmap_generator.php为模块生成新的类映射来解决此问题
<?php echo $this->headMeta(); ?>
$this->headMeta("test description text", "description");