ARTICLE

symfony1.4.2 で Open Graph protocol(OGP)を追加する

※このときは全く symfony1.4 を理解しておらずこんな対応してしまいました…、普通の対応方法はこちらを参考ください。 symfony1.4 で slot で Open Graph protocol(OGP)に対応する


Facebook のシェアに対応するべく symfony に Open Graph Protocol 機能を追加しました。
プラグインの書き方とか分からんから本体弄ったアホです・・・

lib/vendor/symfony/response/sfWebResponse.class.php に以下を追加・・・
※protected のメンバー変数に $metaProps も付け加えてね。

/**
 * Retrieves all meta[property] headers.
 *
 * @return array List of meta headers
 */
public function getMetaProps()
{
  return $this->metaProps;
}

/**
 * Adds a open graph protocol header.
 *
 * @param string  $key      Name of the header
 * @param string  $value    Meta header value (if null, remove the meta)
 * @param bool    $replace  true if it's replaceable
 * @param bool    $escape   true for escaping the header
 */
public function addMetaProp($key, $value, $replace = true, $escape = true)
{
  $key = strtolower($key);

  if (null === $value)
  {
    unset($this->metaProps[$key]);

    return;
  }

  // FIXME: If you use the i18n layer and escape the data here, it won't work
  // see include_metas() in AssetHelper
  if ($escape)
  {
    $value = htmlspecialchars($value, ENT_QUOTES, $this->options['charset']);
  }

  $current = isset($this->metaProps[$key]) ? $this->metas[$key] : null;
  if ($replace || !$current)
  {
    $this->metaProps[$key] = $value;
  }
}

lib/vendor/symfony/helper/AssetHelper.php に以下を追加・・・

function include_meta_props()
{
  $context = sfContext::getInstance();
  $i18n = sfConfig::get('sf_i18n') ? $context->getI18N() : null;
  foreach ($context->getResponse()->getMetaProps() as $name => $content)
  {
    echo tag('meta', array('property' => $name, 'content' => null === $i18n ? $content : $i18n->__($content)))."\n";
  }
}

あとは action のほうで以下を

$this->getResponse()->addMetaProp('og:title', 'hogehoge');
$this->getResponse()->addMetaProp('og:type', 'article');
$this->getResponse()->addMetaProp('og:url', 'http://hogehogehogehogeh');
$this->getResponse()->addMetaProp('og:site_name', 'hogehoge');
$this->getResponse()->addMetaProp('og:description', 'hogehoge');

template のほうで以下を

include_meta_props();

はい、こんな対応は駄目ですよー。こちらを参考にしてねー。
symfony1.4 で slot で Open Graph protocol(OGP)に対応する

blog comments powered by Disqus
  1. tetsuwo posted this

Categories