0

我在 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');

但没有一个奏效。有什么帮助吗?

4

1 回答 1

2

那是因为 append 方法不会在已经定义的图像上添加 og:image。append 方法会将下一个 og:image 附加到已经定义的 og:image 中,最终得到一个数组。同样,如果您选择 prepend,这会将您的新 og:image 标签添加到您已经定义的标签之前。

我认为您真正想要的是最初设置属性,而不是附加它。

$hm =  $serviceManager->get('ViewHelperManager')->get('headMeta');
$hm->setProperty('og:image', '/images/opengraph/1.jpg');

然后稍后:

$hm->setProperty('og:image', '/images/opengraph/2.jpg');

甚至更晚:

$hm->setProperty('og:image', '/images/opengraph/2.jpg');

If you absolutely want to blow away the list, as a last resort you can see this link: Zend Framework: Clearing/Resetting HeadLink, HeadMeta, HeadScript, HeadStyle and HeadTitle based on ZF1 but looking at the ZF2 code appears to be the same.

Perhaps a better solution to the overall challenge is not eagerly set the og:image's in the module, but to always set them as late as possible so you are not in this situation.

于 2013-03-30T02:30:21.803 回答