bahmanshams / shareable-link
方便地生成适用于各种社交媒体网站的分享链接
1.0.2
2020-04-09 04:35 UTC
Requires
- php: ^7.2
Requires (Dev)
- phpunit/phpunit: ^7.2
This package is auto-updated.
Last update: 2024-09-09 16:57:44 UTC
README
介绍
使用每种社交媒体网站自身链接的语法,方便地生成各种不同的社交媒体网站的分享链接。
安装
您可以使用以下命令通过Composer安装此包
composer require bahmanshams/shareable-link:^1.0.0
示例用法
$url = new \BahmanShams\ShareableLink('http://example.com/', 'Example Site'); // Alternatively, with the helper function: // shareable_link('http://example.com/', 'Example Site'); echo $url->facebook; // https://#/dialog/share?app_id=ABC123&href=https://example.com/&display=page&title=Example+Site echo $url->twitter; // https://twitter.com/intent/tweet?url=https://example.com/&text=Example+Site echo $url->whatsapp; // https://wa.me/?text=Example+Site+https%3A%2F%2Fexample.com%2F echo $url->linkedin; // https://www.linkedin.com/sharing/share-offsite?url=https://example.com
Facebook链接注释
要通过Facebook分享的链接需要平台的应用ID。默认情况下,这会尝试通过FACEBOOK_APP_ID
环境变量获取。但是,如果此环境变量不存在,或者您需要为不同的URL传递不同的应用ID,您可以通过显式地传递到getFacebookUrl()
方法。
$url = new \BahmanShams\ShareableLink('http://example.com/', 'Example Site'); putenv('FACEBOOK_APP_ID=ABC123'); echo $url->facebook; // https://#/dialog/share?app_id=ABC123&href=https://example.com/&display=page&title=Example+Site echo $url->getFacebookUrl('XYZ789'); // https://#/dialog/share?app_id=XYZ789&href=https://example.com/&display=page&title=Example+Site
Laravel模型特性
如果您正在使用Laravel并且希望直接从Eloquent模型中使用这些方法作为方便的方法,您可以简单地创建一个。
这样做的好处是,您可以完全控制要使用的URL和标题,同时仍然保持流畅的语法。
class News extends Model { public function getShareUrlAttribute(): \BahmanShams\ShareableLink { $url = route('news.show', $this->slug); return new \BahmanShams\ShareableLink($url, $this->title); } }
然后您可以像使用正常类一样使用伪属性方法。
$news->share_url->twitter;
致谢
- Liam Hammett,感谢他的原始文章,这促使了本文的写作
- Denis Smink,感谢他的原始文章,这促使了本文的写作