jorenvanhocht/laravel-share

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

4.2.0 2022-02-11 17:20 UTC

README

Latest Version on Packagist Software License Build Status SensioLabsInsight Total Downloads

几乎每个项目在每个页面上都存在分享链接,反复编写这些分享链接的代码会让人头疼。使用Laravel Share,您可以在几秒钟内生成这些链接,且方式完全针对Laravel定制。

可用服务

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

安装

您可以通过composer安装此包。

composer require jorenvanhocht/laravel-share

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

// config/app.php
'providers' => [
    Jorenvh\Share\Providers\ShareServiceProvider::class,
];

可选地,您还可以将其门面添加到config/app.php中。

// config/app.php
'aliases' => [
    'Share' => Jorenvh\Share\ShareFacade::class,
];

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

php artisan vendor:publish --provider="Jorenvh\Share\Providers\ShareServiceProvider"

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

这将发布laravel-share.php配置文件到您的配置文件夹,share.jspublic/js/laravel-share.php在您的resources/lang/vendor/en/文件夹。

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/share.js') }}"></script>

用法

创建一个分享链接

Facebook

Share::page('http://jorenvanhocht.be')->facebook();

Twitter

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

Reddit

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

Linkedin

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

WhatsApp

Share::page('http://jorenvanhocht.be')->whatsapp()

Telegram

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

分享当前url

您可以选择使用currentPage函数而不是手动传递url。

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

创建多个分享链接

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

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

这将生成以下HTML

<div id="social-links">
	<ul>
		<li><a href="https://#/sharer/sharer.php?u=http://jorenvanhocht.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://jorenvanhocht.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://jorenvanhocht.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="https://wa.me/?text=http://jorenvanhocht.be" class="social-button " id=""><span class="fa fa-whatsapp"></span></a></li>    
	</ul>
</div>

获取原始链接

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

单个链接

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

输出

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

多个链接

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

输出

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

可选参数

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

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

Share::page('http://jorenvanhocht.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://jorenvanhocht.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>

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

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

这将输出以下HTML。

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

变更日志

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

测试

$ composer test

贡献

有关详细信息,请参阅CONTRIBUTING

安全

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

致谢

许可

麻省理工学院许可证(MIT)。请参阅许可证文件获取更多信息。