beyerz / open-graph-protocol-bundle
OpenGraph 协议和 Symfony2 的集成
2.0.0
2016-12-22 09:10 UTC
Requires
- php: >=5.5
- symfony/symfony: ~2.8|~3.0
Requires (Dev)
This package is auto-updated.
Last update: 2024-09-18 20:12:27 UTC
README
OpenGraphProtocolBundle
Symfony2 项目集成 OpenGraph 协议的简易解决方案
OpenGraphBundle 使 Symfony2 和 twig 视图轻松集成 Open Graph Protocols
重要提示:如果您使用的是低于 2.8 版本的 symfony,请继续使用 v1.0 版本,因为 v2.0 可能由于 symfony 使用 traits 进行容器感知而不正确工作
安装
Composer
composer require beyerz/open-graph-protocol-bundle
应用程序内核
将 OpenGraphBundle 添加到您应用程序内核的 registerBundles()
方法中
public function registerBundles() { return array( new Beyerz\OpenGraphProtocolBundle\OpenGraphProtocolBundle(), ); }
配置
通过向应用程序的 config.yml
文件添加以下内容来启用加载 OGP 服务和设置默认值
目前支持基础和 Facebook 库,但您可以添加尽可能多的库和默认值
#OGP Bundle open_graph_protocol: libraries: base: class: Beyerz\OpenGraphProtocolBundle\Libraries\Base default_values: site_name: {{ default value for website name }} type: {{ default value for website type }} title: {{ default value for any page title }} url: {{ default value for any canonical url (acts as a fall back for bad pages) }} image: {{ default image for your site }} description: {{ default generic page description for your site }} facebook: class: Beyerz\OpenGraphProtocolBundle\Libraries\Facebook default_values: { app_id: {{ Your apps facebook id }} }
文档
查看
在布局的 head
标签中包含 OGP Metas。
使用 twig
{% block metas %}
{{ ogp()|raw }}
{% endblock %}
重写元值
通常,您可能想要更改标题、图像、描述等元值。这很容易在页面控制器内完成(或任何可以访问服务容器的位置)
从您的控制器
$ogp = $this->get('beyerz.ogp.open_graph');
$base = $ogp->get('base');
$base->addMeta('title', "My dynamic title");
$base->addMeta('url', $request->getSchemeAndHttpHost().$request->getRequestUri());
$base->addMeta('description', "My dynamic description");