支持Laravel.io的持续发展 →

启动PHP GitHub赞助者

2021年8月28日 阅读时间:3分钟

非常高兴地宣布,我和Tom终于推出了PHP GitHub Sponsors,一个用于PHP与GitHub Sponsors GraphQL API交互的包。

目前,PHP GitHub Sponsors允许您进行基于ACL的检查,以查看一个GitHub账户是否在赞助另一个账户。虽然该包与框架无关,因此可以在任何PHP应用程序中使用,但它还提供了对Laravel框架的集成。

以下是使用示例。首先,在您的.env文件中设置个人访问令牌

GH_SPONSORS_TOKEN=ghp_xxx

此访问令牌应具有user:readorganization:read范围。

然后,您可以开始使用此包

use GitHub\Sponsors\Facades\GitHubSponsors;

// Check if driesvints is being sponsored by nunomaduro...
GitHubSponsors::login('driesvints')->isSponsoredBy('nunomaduro');

// Check if the blade-ui-kit organization is sponsored by nunomaduro...
GitHubSponsors::login('nunomaduro')->isSponsoring('blade-ui-kit');

// Check if the authenticated user is sponsored by Gummibeer...
GitHubSponsors::viewer()->isSponsoredBy('Gummibeer');

// Check if the authenticated user is sponsoring driesvints...
GitHubSponsors::viewer()->isSponsoring('driesvints');

您还可以从IoC容器中检索客户端并对其进行调用

use GitHub\Sponsors\Client;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;

Route::get('/home', function (Request $request, Client $sponsors) {
    $isSponsoring = $sponsors->viewer()->isSponsoredBy(
        $request->get('github_username')
    );

    if ($isSponsoring) {
        return redirect('/secret-homepage');
    }

    return view('welcome');
});

可赞助

此外,您可以将GitHub\Sponsors\Concerns\Sponsorable特性添加到Eloquent模型,例如User模型,以使其可赞助

use GitHub\Sponsors\Concerns\Sponsorable;
use GitHub\Sponsors\Contracts\Sponsorable as SponsorableContract;
use Illuminate\Database\Eloquent\Model;

class User extends Model implements SponsorableContract
{
    use Sponsorable;
}

然后,用户将使用其github列的值作为其用户名。用户还可以在github_token列中添加他们的个人访问令牌,以让他们检查他们的私人赞助。

现在,您可以像这样使用该模型

$user = User::where('github', 'driesvints')->first();

// Check if driesvints is being sponsored by nunomaduro...
$user->isSponsoredBy('nunomaduro');

// Check if driesvints is sponsoring nunomaduro...
$user->isSponsoring('driesvints');

有关使用方法和详细信息,请参阅文档

路线图

最初,我想在包中提供更多功能。但由于我只能在过去几个月间零零散散地工作,感觉进展不够快。因此,我决定提前发布,提供一个最小功能集,并继续在公开场合对其进行开发。

我们想在包中实现很多计划。以下是我们的路线图上的一些功能

结论

这个包不可能没有一些杰出的人。我真的很想感谢 Claudio Dekker 在开发包期间提供的所有反馈和帮助。我还想感谢 Sarah Vessels 提供的GitHub GraphQL API反馈。最后,我想感谢 Tom Witkowski 作为共同维护者加入我,并帮助开发和改进这个包。

我希望你会喜欢这个,如果你最终使用了这个包,请一定在 Twitter上告诉我。祝您享受!

上次更新1年前。

opi, joedixon, alexanderfalkenberg 喜欢这篇文章

3
喜欢这篇文章? 让作者知道并给予他们掌声!
driesvints (Dries Vints) 我在Laravel工作并维护Laravel.io。Eventy的创建者。

你可能还会喜欢的其他文章

March 11th 2024

如何使用Larastan使您的Laravel应用程序从0到9

在您的Laravel应用程序执行之前发现错误是可能的,这要归功于Larastan...

阅读文章
July 19th 2024

无需特性的标准化API响应

我注意到大多数为API响应创建的库都是使用特性实现的...

阅读文章
2024年7月17日

在您的Laravel项目中通过Discord通知收集反馈

如何在Laravel项目中创建反馈模块,并在接收到一条消息时接收Discord通知……

阅读文章

我们要感谢这些 极佳的公司 支持我们

您的标志在这里?

Laravel.io

Laravel的问题解决、知识共享和社区建设门户。

© 2024 Laravel.io - 版权所有。