hebrahimzadeh / laravel-share
使用 Laravel 分享链接
v1.0.2
2022-10-21 08:01 UTC
Requires
- php: ^7.4 || ^8.0 || ^8.1
- illuminate/support: ^6 || ^7 || ^8 || ^9
Requires (Dev)
- orchestra/testbench: ^4 || ^5 || ^6 || ^7
- rector/rector: ^0.14.x-dev
- tightenco/tlint: dev-main
This package is auto-updated.
Last update: 2024-09-21 12:29:52 UTC
README
使用 Laravel 分享链接
可用的服务
- 博客作者 : blogger
- Digg : digg
- 电子邮件 : email
- Evernote : evernote
- Facebook : facebook
- Gmail : gmail
- LinkedIn : linkedin
- Pinterest : pinterest
- Reddit : reddit
- Scoop.it : scoopit
- Telegram.me : telegram
- Tumblr : tumblr
- Twitter : twitter
- vk.com : vk
安装
将 Composer 依赖项安装到您的项目中
composer require hebrahimzadeh/laravel-share
使用方法
获取链接(以 Twitter 为例)
Route::get('/', function() { return Share::page('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::page('http://www.example.com', 'Link description')->services('facebook', 'twitter')->getLinks(); });
返回数组
{ "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::page('http://www.example.com', 'Link description')->services(); });
返回所有定义的服务的结果数组。
自定义
发布包配置
php artisan vendor:publish --provider='Hebrahimzadeh\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 的唯一方法(如果已配置为这种方式)。
升级
当包升级时,应将配置和视图更改重新发布到您的项目中
php artisan vendor:publish --provider='Hebrahimzadeh\Share\ShareServiceProvider'
如果您已自定义文件,请使用源代码控制来确定发生了什么更改。