この記事は、「フォロー」機能をStreamを使用してLaravelアプリケーションに追加することに関する以前のチュートリアルに基づいています。 この部分は、アクティビティ追跡のモデルの構成、ストリームのフィードタイプの探索、フィードの取得、およびビューでそれらをレンダリングすることに焦点を当てています。
。
重要な概念:
FeedManager
はフィード操作を簡素化し、事前に構築されたフィード(ユーザー、ニュース、通知)を提供します。は
feedmanager を使用/followingに使用し、それに応じてフィードを更新します。
アクティビティデータ構造:
ストリームは、少なくとも俳優、動詞、オブジェクト、および時間のアクティビティとしてデータを表します。 カスタムフィールドも許可されています。
Activityverb を
post モデル:
で定義する
class Post extends Model
{
// ... other code ...
/**
* Stream: Activity verb for post creation.
*/
public function activityVerb()
{
return 'created';
}
}
フィードマネージャーを利用する:
feedmanager はフィードの相互作用を簡素化します。
config/app.php 。
に設定されたファサードエイリアスからアクセスされます。
事前に構成されたフィード:
feedmanager は、事前に構築されたフィードを提供します:ユーザー、ニュース、および通知。 この例では、主にニュースと通知フィードを使用しています。 他のフィードタイプの詳細は、こちら
で利用できます。。
the followcontroller
が更新され、 feedmanager
を使用して、効率的なフォロー/フローアクション:
// app/Http/Controllers/FollowController.php
public function follow(User $user)
{
if (!Auth::user()->isFollowing($user->id)) {
Auth::user()->follows()->create(['target_id' => $user->id]);
FeedManager::followUser(Auth::id(), $user->id);
return back()->with('success', 'Now following ' . $user->name);
} else {
return back()->with('error', 'Already following this user.');
}
}
public function unfollow(User $user)
{
if (Auth::user()->isFollowing($user->id)) {
$follow = Auth::user()->follows()->where('target_id', $user->id)->first();
FeedManager::unfollowUser(Auth::id(), $follow->target_id);
$follow->delete();
return back()->with('success', 'Unfollowed ' . $user->name);
} else {
return back()->with('error', 'Not following this user.');
}
}
フィードの表示:a
feedscontrollerは、フィード検索と表示を処理するために作成されています:
// app/Http/Controllers/FollowController.php
public function follow(User $user)
{
if (!Auth::user()->isFollowing($user->id)) {
Auth::user()->follows()->create(['target_id' => $user->id]);
FeedManager::followUser(Auth::id(), $user->id);
return back()->with('success', 'Now following ' . $user->name);
} else {
return back()->with('error', 'Already following this user.');
}
}
public function unfollow(User $user)
{
if (Auth::user()->isFollowing($user->id)) {
$follow = Auth::user()->follows()->where('target_id', $user->id)->first();
FeedManager::unfollowUser(Auth::id(), $follow->target_id);
$follow->delete();
return back()->with('success', 'Unfollowed ' . $user->name);
} else {
return back()->with('error', 'Not following this user.');
}
}
encrich メソッドは、ビューレンダリングのデータ変換を処理します。 このコントローラーアクションにアクセスするためにルートが定義されています。
newsfeed
は、個々のアクティビティをレンダリングするために部分的な( render_activity
)を使用して、強化されたアクティビティを繰り返します。 カスタムアクティビティパーティシャル(例: created.blade.php
for post recument)は、アクティビティ
View Folder。内で作成されます。
例
partial:
{{ date('F j, Y, g:i a', strtotime($activity['time'])) }}
{{ $activity['actor']['name'] }} created a new post titled {{ $activity['object']['title'] }}
通知フィード:
フォローモデルが更新され、通知フィード処理が含まれる:
class Follow extends Model
{
use \GetStream\StreamLaravel\Eloquent\ActivityTrait;
// ... other code ...
public function activityNotify()
{
$targetFeed = FeedManager::getNotificationFeed($this->target_id);
return [$targetFeed];
}
public function activityVerb()
{
return 'follow';
}
public function activityExtraData()
{
return ['followed' => $this->target, 'follower' => $this->user];
}
}
同様のコントローラーアクション、ルート、およびビュー( notifications.blade.php )が作成されます。 個別の部分(
notification_follow.blade.php )がフォロー通知に使用されます。
結論:
ストリームは、Laravelアプリケーションに堅牢なフィード機能を追加することを簡素化します。 チュートリアルでは、さまざまなアクティビティを追跡し、さまざまなフィードタイプを管理し、ビューで効率的にレンダリングする方法を示しています。 Streamの機能のさらなる調査が奨励されています。
faqs(わずかに再フォーマット):
FAQSセクションはよく書かれており、役立つ情報を提供します。 重要な変更は必要ありませんが、マイナーなフォーマット調整により、読みやすさが向上する可能性があります。 より良いビジュアル組織のために、番号付きリストまたは太字の重要な用語を使用することを検討してください。免責事項: 提供されるすべてのリソースの一部はインターネットからのものです。お客様の著作権またはその他の権利および利益の侵害がある場合は、詳細な理由を説明し、著作権または権利および利益の証拠を提出して、電子メール [email protected] に送信してください。 できるだけ早く対応させていただきます。
Copyright© 2022 湘ICP备2022001581号-3