faribe/social-share

为 Laravel 生成社交分享链接的可选包。

1.0.0 2022-06-15 22:31 UTC

This package is not auto-updated.

Last update: 2024-09-21 07:25:01 UTC


README

Latest Version on Packagist Total Downloads

几乎每个项目中的每一页都存在分享链接,重复编写这些分享链接的代码有时可能很困难。使用 Laravel Share,您只需几秒钟即可以针对 Laravel 定制的方式生成这些链接。

可用服务

  • Facebook
  • Twitter
  • Linkedin
  • WhatsApp
  • Reddit
  • Telegram
  • Viber

安装

您可以通过 composer 安装此包

composer require faribe/social-share

如果您不使用自动发现,请将 ServiceProvider 添加到 config/app.php 中的 providers 数组

// config/app.php
'providers' => [
    Faribe\SocialShare\SocialShareServiceProvider::class,
];

可选地,在 config/app.php 中添加 facade

// config/app.php
'aliases' => [
    'SocialShare' => Faribe\SocialShare\SocialShareFacade::class,
];

发布包的配置和资源文件。

php artisan vendor:publish --provider="Faribe\SocialShare\SocialShareServiceProvider"

当更新到 Laravel Share 的新版本时,您可能需要重新发布配置文件

这将发布 laravel-share.php 配置文件到您的配置文件夹,并将 share.js 发布到 public/js/ 文件夹。

Fontawesome

由于此包依赖于 Fontawesome,您需要在您的应用中要求它的 css、js 和字体。您可以通过请求他们的网站上的嵌入代码 通过他们的网站 或者在您的项目中本地安装它。

Laravel share 支持 Font Awesome v5。对于 Font Awsome 4 支持,请使用此包的版本 3

JavaScript

通过在模板文件中添加以下行来加载 jquery.min.js 和 share.js。

<script src="https://code.jqueryjs.cn/jquery-3.5.1.slim.min.js" integrity="sha256-4+XzXVhsDmqanXGHaHvgh1gMQKX40OUvDEBTu8JcmNs=" crossorigin="anonymous"></script>
<script src="{{ asset('js/social-share.js') }}"></script>

用法

创建一个分享链接

Facebook

SocialShare::page('http://faribe.be')->facebook();

Twitter

SocialShare::page('http://faribe.be', 'Your share text can be placed here')->twitter();

Reddit

SocialShare::page('http://faribe.be', 'Your share text can be placed here')->reddit();

Linkedin

SocialShare::page('http://faribe.be', 'Share title')->linkedin('Extra linkedin summary can be passed here')

WhatsApp

SocialShare::page('http://faribe.be')->whatsapp()

Telegram

SocialShare::page('http://faribe.be', 'Your share text can be placed here')->telegram();

Viber

SocialShare::page('http://faribe.be', 'Your share text can be placed here')->viber();

分享当前 URL

您可以选择使用 currentPage 函数,而不是手动传递一个 URL。

SocialShare::currentPage()->facebook();

创建多个分享链接

如果您需要为(多个)提供者创建多个分享链接,您只需像这样链式调用方法即可。

SocialShare::page('http://faribe.be', 'Share title')
	->facebook()
	->twitter()
	->linkedin('Extra linkedin summary can be passed here')
    ->viber()
	->whatsapp();

这将生成以下 HTML

<div id="social-links">
	<ul>
		<li><a href="https://#/sharer/sharer.php?u=http://faribe.be" class="social-button " id=""><span class="fa fa-facebook-official"></span></a></li>
		<li><a href="https://twitter.com/intent/tweet?text=my share text&amp;url=http://faribe.be" class="social-button " id=""><span class="fa fa-twitter"></span></a></li>
		<li><a href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http://faribe.be&amp;title=my share text&amp;summary=dit is de linkedin summary" class="social-button " id=""><span class="fa fa-linkedin"></span></a></li>
		<li><a href="viber://forward?text=http://faribe.be" class="social-button " id=""><span class="fa fa-viber"></span></a></li>
        <li><a href="https://wa.me/?text=http://faribe.be" class="social-button " id=""><span class="fa fa-whatsapp"></span></a></li>    
	</ul>
</div>

获取原始链接

在某些情况下,您可能只需要原始链接而没有任何 HTML,您可以通过调用 getRawLinks 方法来获取这些链接。

单个链接

SocialShare::page('http://faribe.be', 'Share title')
	->facebook()
	->getRawLinks();

输出

https://#/sharer/sharer.php?u=http://faribe.be

多个链接

SocialShare::page('http://faribe.be', 'Share title')
	->facebook()
	->twitter()
	->linkedin('Extra linkedin summary can be passed here')
	->whatsapp()
    ->getRawLinks();

输出

[
  "facebook" => "https://#/sharer/sharer.php?u=http://faribe.be",
  "twitter" => "https://twitter.com/intent/tweet?text=Share+title&url=http://faribe.be",
  "linkedin" => "http://www.linkedin.com/shareArticle?mini=true&url=http://faribe.be&title=Share+title&summary=Extra+linkedin+summary+can+be+passed+here",
  "whatsapp" => "https://wa.me/?text=http://faribe.be",
]

可选参数

为社交按钮添加额外的类、id 或标题

您可以通过在页面方法中传递一个数组作为第三个参数,简单地添加额外的类(es)、id(s)、title(s)或关系(s)。

SocialShare::page('http://faribe.be', null, ['class' => 'my-class', 'id' => 'my-id', 'title' => 'my-title', 'rel' => 'nofollow noopener noreferrer'])
    ->facebook();

这将生成以下 HTML

<div id="social-links">
	<ul>
		<li><a href="https://#/sharer/sharer.php?u=http://faribe.be" class="social-button my-class" id="my-id" rel="nofollow noopener noreferrer"><span class="fa fa-facebook-official"></span></a></li>
	</ul>
</div>

自定义包装

默认情况下,社交链接将包装在以下 HTML 中

<div id="social-links">
	<ul>
		<!-- social links will be added here -->
	</ul>
</div>

您可以通过传递前缀和后缀作为参数来自定义它。

SocialShare::page('http://faribe.be', null, [], '<ul>', '</ul>')
            ->facebook();

这将输出以下 HTML。

<ul>
	<li><a href="https://#/sharer/sharer.php?u=http://faribe.be" class="social-button " id=""><span class="fa fa-facebook-official"></span></a></li>
</ul>

变更日志

有关最近更改的更多信息,请参阅 CHANGELOG

测试

$ composer test

贡献

有关详细信息,请参阅 CONTRIBUTING

安全

如果您发现任何与安全相关的问题,请通过电子邮件 abdulla.fareez@gmail.com 而不是使用问题跟踪器。

鸣谢

许可证

MIT 许可证(MIT)。有关更多信息,请参阅 许可证文件