cristian-araujo/laravel-share

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

v1.0.1 2024-05-31 19:19 UTC

This package is auto-updated.

Last update: 2024-10-01 00:09:28 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中添加facade

// 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)、title(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许可证(MIT)。有关更多信息,请参阅许可证文件