我在Module.php中设置了几个默认的元参数:
$hm = $serviceManager->get('ViewHelperManager')->get('headMeta');
$hm
->appendProperty('og:image', '/images/opengraph/1.jpg')
->appendProperty('og:image', '/images/opengraph/2.png');
稍后在一些动作中,我需要覆盖这些元参数并设置一些其他图像。我不知道如何清除这些现有的'og:image'参数…我试过了:
$hm->appendProperty('og:image', null);
//and
$hm->unsetProperty('og:image');
但是都不起作用。任何帮助吗?
这是因为append方法不会在已经定义的og:image上添加og:image。append方法将把下一个og:image附加到已经定义的og:image上,最终得到一个og:image数组。类似地,如果选择prepend,这将在.
中已经定义的og:image标记之前添加新的og:image标记。我认为你真正想要的是一开始就设置属性,而不是附加它。
$hm = $serviceManager->get('ViewHelperManager')->get('headMeta');
$hm->setProperty('og:image', '/images/opengraph/1.jpg');
then later:
$hm->setProperty('og:image', '/images/opengraph/2.jpg');
或更迟:
$hm->setProperty('og:image', '/images/opengraph/2.jpg');
如果你绝对想吹走列表,作为最后的手段,你可以看到这个链接:Zend框架:清除/重置HeadLink, HeadMeta, HeadScript, HeadStyle和HeadTitle基于ZF1,但看着ZF2代码似乎是相同的。
也许对整体挑战的更好解决方案不是急切地在模块中设置og:image,而是总是尽可能晚地设置它们,这样您就不会遇到这种情况。