在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):GetStream/stream-laravel开源软件地址(OpenSource Url):https://github.com/GetStream/stream-laravel开源编程语言(OpenSource Language):PHP 97.1%开源软件介绍(OpenSource Introduction):Stream Laravelstream-laravel is a Laravel client for Stream. You can use this in any Laravel application, or in any application that uses Eloquent ORM ( You can sign up for a Stream account at https://getstream.io/get_started. Note there is also a lower level PHP - Stream integration library which is suitable for all PHP applications. Build Activity Streams, News Feeds, and MoreYou can build:
Demoshttps://github.com/GetStream/Stream-Laravel-Example https://github.com/GetStream/Stream-Example-PHP InstallationComposerBegin by installing this package through Composer. Edit your project's
Next, update Composer:
LaravelLaravel prior to 5.5 (no longer supported) Add
And add the
Publish the configuration file:
This will create GetStream.io DashboardNow, login to GetStream.io and create an application in the dashboard. Retrieve the API key, API secret, and API app id, which are shown in your dashboard. Create feeds in your new application. By default, you should create the following:
Stream-Laravel Config FileSet your key, secret, and app id in
You can also set the name of your feeds here:
And that should get you off and running with Stream-Laravel. Have lots of fun! Lumen InstallationBegin by installing this package through Composer.
Add $app->register(\GetStream\StreamLaravel\StreamLumenServiceProvider::class); Manually create a config file in ./config/stream-laravel.php... <?php
return [
'api_key' => 'API_KEY',
'api_secret' => 'API_SECRET',
'api_app_id' => 'API_APP_ID',
'location' => 'us-east',
'timeout' => 3,
]; and tell Lumen to configure it, in bootstrap. $app->configure('stream-laravel'); Features of Stream-LaravelEloquent IntegrationStream-Laravel provides instant integration with Eloquent models - extending the For example: class Pin extends Eloquent {
use GetStream\StreamLaravel\Eloquent\ActivityTrait; Everytime a Pin is created it will be stored in the feed of the user that created it, and when a Pin instance is deleted than it will get removed as well. Automatically! Activity FieldsModels are stored in feeds as activities. An activity is composed of at least the following data fields: actor, verb, object, time. You can also add more custom data if needed. object is a reference to the model instance itself actor is a reference to the user attribute of the instance verb is a string representation of the class name In order to work out-of-the-box the Activity class makes few assumptions:
You can change how a model instance is stored as activity by implementing specific methods as explained later. Below shows an example how to change your class if the model belongs to an author instead of to a user. class Pin extends Eloquent {
use GetStream\StreamLaravel\Eloquent\ActivityTrait;
public function author()
{
return $this->belongsTo('Author');
}
public function activityActorMethodName()
{
return 'author';
} Activity Extra DataOften, you'll want to store more data than just the basic fields. You achieve this by implementing the NOTE: you should only return data that can be serialized by PHP's json_encode function class Pin extends Eloquent {
use GetStream\StreamLaravel\Eloquent\ActivityTrait;
public function activityExtraData()
{
return ['is_retweet' => $this->is_retweet];
} Customize Activity VerbBy default, the verb field is the class name of the activity, you can change that implementing the class Pin extends Eloquent {
use GetStream\StreamLaravel\Eloquent\ActivityTrait;
public function activityVerb()
{
return 'pin';
} Feed ManagerStream Laravel comes with a FeedManager class that helps with all common feed operations. You can get an instance of the manager with Pre-Bundled FeedsTo get you started the manager has feeds pre configured. You can add more feeds if your application needs it. The three feeds are divided in three categories. User Feed:The user feed stores all activities for a user. Think of it as your personal Facebook page. You can easily get this feed from the manager. $feed = FeedManager::getUserFeed($user->id); News Feed:The news feeds store the activities from the people you follow. There is both a timeline (similar to twitter) and an aggregated timeline (like facebook). $timelineFeed = FeedManager::getNewsFeeds($user->id)['timeline'];
$aggregatedTimelineFeed = FeedManager::getNewsFeeds($user->id)['timeline_aggregated']; Notification Feed:The notification feed can be used to build notification functionality. Below we show an example of how you can read the notification feed. notification_feed = FeedManager::getNotificationFeed($user->id); By default the notification feed will be empty. You can specify which users to notify when your model gets created. In the case of a retweet you probably want to notify the user of the parent tweet. class Tweet extends Eloquent {
use GetStream\StreamLaravel\Eloquent\ActivityTrait;
public function activityNotify()
{
if ($this->isRetweet) {
$targetFeed = FeedManager::getNotificationFeed($this->parent->user->id);
return [$targetFeed];
}
} Another example would be following a user. You would commonly want to notify the user which is being followed. class Follow extends Eloquent {
use GetStream\StreamLaravel\Eloquent\ActivityTrait;
public function target()
{
return $this->belongsTo('User');
}
public function activityNotify()
{
$targetFeed = FeedManager::getNotificationFeed($this->target->id);
return [$targetFeed];
} Follow FeedTo create the newsfeeds you need to notify the system about follow relationships. The manager comes with APIs to let a user's news feeds follow another user's feed. This code lets the current user's timeline and timeline_aggregated feeds follow the target_user's personal feed.
Displaying the NewsfeedActivity EnrichmentWhen you read data from feeds, a like activity will look like this:
This is far from ready for usage in your template. We call the process of loading the references from the database enrichment. An example is shown below:
The enrich method returns an array of objects of type On your model: use App\Transformers\MyModelEnrichTransformer;
use GetStream\StreamLaravel\Eloquent\ActivityTrait;
use Illuminate\Database\Eloquent\Model;
class MyModel extends Model
{
public function enrichTransformer() {
return new MyModelEnrichTransformer();
}
} In your controller: use GetStream\StreamLaravel\Enrich;
$feed = FeedManager::getNewsFeeds($user->id)['timeline'];
$enricher = new Enrich();
$activities = $feed->getActivities(0, 25)['results'];
$activities = $enricher->enrichActivities($activities);
$collection = new Collection();
foreach ($activities as $activity) {
$record = [
"actor" => $this->transformData($activity["actor"], $activity["actor"]->enrichTransformer()),
"object" => $this->transformData($activity["object"], $activity["object"]->enrichTransformer()),
"verb" => $activity["verb"],
"foreign_id" => $activity["foreign_id"],
"time" => $activity["time"],
];
if (!empty($activity["target"])) {
array_push($record, [
"target" => $this->transformData($activity["target"], $activity["target"]->enrichTransformer()),
]);
}
$collection->push($record);
}
return response()->json($collection); TemplatingNow that you've enriched the activities you can render them in a view. For convenience we includes a basic view:
The For example activity/tweet.blade.php will be used to render an normal activity with verb tweet and aggregated_activity/like.blade.php for an aggregated activity with verb like If you need to support different kind of templates for the same activity, you can send a third parameter to change the view selection. The example below will use the view activity/homepage_like.html
Customizing EnrichmentSometimes you'll want to customize how enrichment works. The documentation will show you several common options. Enrich Extra FieldsIf you store references to model instances in the activity extra_data you can use the Enrich class to take care of it for you:
Preload Related DataYou will commonly access related objects such as activity['object']->user. To prevent your newsfeed to run N queries you can instruct the manager to load related objects. The manager will use Eloquent's
Full documentation and Low level APIs accessWhen needed you can also use the low level PHP API directly. Documentation is available at the Stream website.
ContributingWe welcome code changes that improve this library or fix a problem, please make sure to follow all best practices and add tests if applicable before submitting a Pull Request on Github. We are very happy to merge your code in the official repository. Make sure to sign our Contributor License Agreement (CLA) first. See our license file for more details. Getting started: $ composer install
$ ./vendor/bin/phpunit Copyright and License InformationCopyright (c) 2014-2022 Stream.io Inc, and individual contributors. All rights reserved. See the file "LICENSE" for information on the history of this software, terms & conditions for usage, and a DISCLAIMER OF ALL WARRANTIES. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论