定制 chriskonnertz/open-graph 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

chriskonnertz/open-graph

Composer 安装命令:

composer require chriskonnertz/open-graph

包简介

Class that assists in building Open Graph meta tags

README 文档

README

Build Status Monthly Downloads Version GitHub license

Library that assists in building Open Graph meta tags.

Installation

Add chriskonnertz/open-graph to composer.json with a text editor:

"chriskonnertz/open-graph": "~2"

Or via a console:

composer require chriskonnertz/open-graph

In the future use composer update to update to the latest version of Open Graph Builder.

This library requires PHP >=7.0.

Framework Support

Laravel >=5.5 can auto-detect this package so you can ignore this section. In Laravel 5.0-5.4 you have to edit your config/app.php config file. You can either add an alias to the object so you can create a new instance via new OpenGraph() ...

'aliases' => [
    ...
    'OpenGraph' => 'ChrisKonnertz\OpenGraph\OpenGraph',
],

...or an alias to the facade (this is what happens in Laravel >=5.5 via package auto-discovery) so you do not have to create the instance by yourself but you can access it via pseudo-static methods. If you choose this path you also have to add the service provider to the config file:

'aliases' => [
    ...
    'OpenGraph' => 'ChrisKonnertz\OpenGraph\OpenGraphFacade',
],

...

'providers' => [
    ...
    'ChrisKonnertz\OpenGraph\OpenGraphServiceProvider',
],

If you need to reset the underlying instance of the facade (the OpenGraph object), call OpenGraph::clear().

Introduction

Example:

$og = OpenGraph::title('Apple Cookie')
    ->type('article')
    ->image('http://example.org/apple.jpg')
    ->description('Welcome to the best apple cookie recipe never created.')
    ->url();

Render these tags in a template as follows:

{!! $og->renderTags() !!}

Providing Open Graph tags enriches web pages. The downside is some extra time to spend, because every model has its own way to generate these tags. It's also important to follow the official protocol. Read the documentation to learn more about the tags that are available and the values they support or check out examples. Please note that this implementation sticks to the specification of OGP.me and does not support the enhancements created by Facebook.

Add Tags And Attributes

Add Basic Tags

$og->title('Apple Cookie')
    ->type('article')
    ->description('A delicious recipe')
    ->url()
    ->locale('en_US')
    ->localeAlternate(['en_UK'])
    ->siteName('Cookie Recipes Website')
    ->determiner('an');

If no argument is passed to the url method the current URL is applied. Note that the environment variable APP_URL is considered if it is set. Furthermore, when executed via CLI, and APP_URL is not set, the domain will be localhost.

Note that DateTime objects will be converted to ISO 8601 strings.

Add Tags With Attributes

You may add image, audio or video tags and pass the basic value (the URL to the object) and an array of additional attributes.

$og->image($imageUrl, [
    'width'     => 300,
    'height'    => 200
]);

$og->audio($audioUrl, [
    'type'     => 'audio/mpeg'
]);

$og->video($videoUrl, [
    'width'     => 300,
    'height'    => 200,
    'type'      => 'application/x-shockwave-flash'
]);

Add Type Attributes

Some object types (determined by the type tag) have their own tags with attributes but not a basic tag. These are article, book and profile.

$og->article([
    'author'        => 'Jane Doe'
]);

$og->book([
    'author'        => 'John Doe'
]);

$og->profile([
    'first_name'    => 'Kim',
    'last_name'     => 'Doe'
]);

Add Attributes

Facebook supports more than just the basic object types. To add attributes for off-the-record object types you may use the attributes method.

Without custom validation rule:

$og->attributes('product', ['product:color' => 'red']);

With custom validation rule:

$og->attributes('product', ['product:color' => 'red'], ['product:color']);

The only validation this method performs is to check if all attribute names match with the list of attribute names.

Add A Tag Several Times

A property can have multiple values. Add the tag several times to achieve this effect.

$og->image('http://example.org/apple.jpg')
    ->image('http://example.org/tree.jpg');

Adding a basic tag a second time will override the value of the first tag. Basic tags must not exist several times.

Validation

If validation is enabled (default is disabled) adding tags will trigger validation. Validation is not covering the complete specification but some important parts. If validation fails the method will throw an exception.

Validation checks if tag values are legit and if attribute types are known.

Enable validation by method:

$og->validate();

By constructor:

$og = new OpenGraph(true);

Disable validation:

$og->validate(false);

Miscellaneous

Determine If A Tag Exists

$hasTitle = $og->has('title');

Remove A Tag From The List

$og->forget('title');

Remove All Tags From The List

$og->clear();

Add A Custom Tag

$og->tag('apples', 7);

To disable auto-prefixing pass a third parameter: $og->tag('apples', 7, false)

Get The Last Tag (By Name)

$tag = $og->lastTag('image');
$value = $tag['value'];

Tags are stored as arrays consisting of name-value-pairs.

Status

Status of this repository: Maintained. If you create an issue you will get a response usually within 48 hours.

chriskonnertz/open-graph 适用场景与选型建议

chriskonnertz/open-graph 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 452.15k 次下载、GitHub Stars 达 116, 最近一次更新时间为 2014 年 03 月 26 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「opengraph」 「open graph」 「laravel」 「ogp」 「laravel5」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 chriskonnertz/open-graph 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 chriskonnertz/open-graph 我们能提供哪些服务?
定制开发 / 二次开发

基于 chriskonnertz/open-graph 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 452.15k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 117
  • 点击次数: 34
  • 依赖项目数: 3
  • 推荐数: 0

GitHub 信息

  • Stars: 116
  • Watchers: 3
  • Forks: 15
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-03-26