hilioski/social-share-and-meta-generator

使用Laravel 5分享链接和元数据生成器

5.2.4 2016-08-30 18:38 UTC

This package is not auto-updated.

Last update: 2024-09-17 04:53:12 UTC


README

使用Laravel 5分享链接和元数据生成器

这是对Checha share for Laravel 5的分支。

可用的服务

  • Delicious : delicious
  • Digg : digg
  • Email : email
  • Evernote : evernote
  • Facebook : facebook
  • Gmail : gmail
  • Google Plus : gplus
  • LinkedIn : linkedin
  • Pinterest : pinterest
  • Reddit : reddit
  • Scoop.it : scoopit
  • Tumblr : tumblr
  • Twitter : twitter
  • Viadeo : viadeo
  • vk.com : vk

安装

步骤1 : 安装Composer依赖项

composer require hilioski/social-share-and-meta-generator

步骤2 : 注册服务提供者

Chencha\Share\ShareServiceProvider添加到config/app.php文件中的providers数组中

步骤3 : 注册别名

Share => Chencha\Share\ShareFacade添加到config/app.php文件中的aliases数组中

用法

生成OG元数据

Share::generateMeta(
    'http://www.example.com',
    'website',
    'My example',
    'http://www.example.com/img/header-bg.jpg',
    'Example.com',
    'Some description about the page'
);

generateMeta()函数的定义是

generateMeta($url = '', $type = 'website', $title = '', $image = '', $site_name = '', $description = '')

获取一个链接(例如Twitter)

Route::get('/', function()
{
	return Share::load('http://www.example.com', 'My example')->twitter();
});

返回一个字符串

https://twitter.com/intent/tweet?url=http%3A%2F%2Fwww.example.com&text=Link+description

获取多个链接

Route::get('/', function()
{
	return Share::load('http://www.example.com', 'Link description')->services('facebook', 'gplus', 'twitter');
});

返回一个数组

{
    "gplus" : "https://plus.google.com/share?url=http%3A%2F%2Fwww.example.com",
    "twitter" : "https://twitter.com/intent/tweet?url=http%3A%2F%2Fwww.example.com&text=Link+description",
    "facebook" : "https://#/sharer/sharer.php?u=http%3A%2F%2Fwww.example.com&title=Link+description"
}

获取所有链接

Route::get('/', function()
{
	return Share::load('http://www.example.com', 'Link description')->services();
});

返回所有定义的服务的结果数组

自定义

发布包配置

php artisan vendor:publish --provider='Chencha\Share\ShareServiceProvider'

在config/social-share.php中添加新的服务

'mynewservice' => [ 'view' => 'share.mynewservice' ]

share.mynewservice视图文件中添加Blade模板代码,以生成mynewservice的URL。您有权限访问

  • service - 服务定义(如上所示)。
  • sep - 参数之间的分隔符,默认为'&'。可配置为social-share.separator
  • url - 正在共享的URL。
  • title - 正在共享的标题。
  • media - 正在共享的媒体链接。

示例

https://mynewservice.example.com?url={{ rawurlencode($url) }}<?php echo $sep; ?>title={{ rawurlencode("Check this out! $title. See it here: $url") }}

对于email服务的另一个示例。将服务配置更改为[ 'view' => 'whatever' ]并将此放入视图文件中

mailto:?subject={{ rawurlencode("Wow, check this: $title") }}<?php echo $sep; ?>body={{ rawurlencode("Check this out! $title. See it here: $url") }}

本地化?简单,使用Laravel的trans()调用

mailto:?subject={{ rawurlencode(trans('share.email-subject', compact('url', 'title', 'media'))) }}<?php echo $sep ?>body={{ rawurlencode(trans('share.email-body', compact('url', 'title', 'media'))) }}

在resources/lang/en/share.php中创建一个文件,包含您选择的主题和正文。URL的最大长度可能为2000个字符。

注意使用。这是打印未编码的ampersand的唯一方式(如果已配置)。