imliam / shareable-link
方便地生成适用于各种社交媒体网站的分享链接
v1.1.0
2022-04-28 15:23 UTC
Requires
- php: ^7.4|^8.1
- ext-mbstring: *
Requires (Dev)
- phpunit/phpunit: ^9.5
This package is auto-updated.
Last update: 2024-08-28 20:32:56 UTC
README
介绍
方便地生成适用于各种不同社交媒体网站的分享链接,使用它们各自的链接语法。
安装
您可以使用以下命令通过Composer安装此包
composer require imliam/shareable-link:^1.0.0
示例用法
$url = new \ImLiam\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/shareArticle?mini=true&url=https://example.com/&summary=Example+Site echo $url->pinterest; // https://pinterest.com/pin/create/button/?media=&url=https://example.com/&description=Example+Site echo $url->google; // https://plus.google.com/share?url=https://example.com/
Facebook链接注释
通过Facebook分享链接需要平台的app ID。默认情况下,这将尝试通过FACEBOOK_APP_ID
环境变量获取。但是,如果此环境变量不存在,或者您需要为不同的URL传递不同的app ID,您可以通过getFacebookUrl()
方法明确传递一个。
$url = new \ImLiam\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(): \ImLiam\ShareableLink { $url = route('news.show', $this->slug); return new \ImLiam\ShareableLink($url, $this->title); } }
然后您可以像使用正常类一样使用伪属性方法。
$news->share_url->twitter;
鸣谢
- Denis Smink,感谢他的原始文章启发了这个项目