「労働者が自分の仕事をうまくやりたいなら、まず自分の道具を研ぎ澄まさなければなりません。」 - 孔子、「論語。陸霊公」
表紙 > プログラミング > LaravelとStreamでソーシャルネットワークを簡単に構築できます

LaravelとStreamでソーシャルネットワークを簡単に構築できます

2025-04-15に投稿されました
ブラウズ:773

この記事は、「フォロー」機能をStreamを使用してLaravelアプリケーションに追加することに関する以前のチュートリアルに基づいています。 この部分は、アクティビティ追跡のモデルの構成、ストリームのフィードタイプの探索、フィードの取得、およびビューでそれらをレンダリングすることに焦点を当てています。

Building a Social Network with Laravel and Stream? Easy!

重要な概念:

  • Stream Laravelの FeedManager はフィード操作を簡素化し、事前に構築されたフィード(ユーザー、ニュース、通知)を提供します。
  • ストリームデータは、アクティビティとしてデータを保存します
  • followcontroller feedmanager を使用/followingに使用し、それに応じてフィードを更新します。
  • アクティビティは
  • 濃縮を表示する前に、生データをビューに優しい形式に変換します。
  • ストリームはさまざまなフィードタイプ(ニュース、通知)をサポートし、さまざまなアクティビティのカスタムテンプレートを許可します。

アクティビティデータ構造:

ストリームは、少なくとも俳優、動詞、オブジェクト、および時間のアクティビティとしてデータを表します。 カスタムフィールドも許可されています。

  • オブジェクト:モデルインスタンスへの参照。
  • 俳優:アクティビティを作成するユーザーへの参照。
  • 動詞:アクションを表す文字列(例: 'created')。
例:

Activityverb post モデル:で定義する

class Post extends Model
{
    // ... other code ...

    /**
     * Stream: Activity verb for post creation.
     */
    public function activityVerb()
    {
        return 'created';
    }
}

フィードマネージャーを利用する:

feedmanager はフィードの相互作用を簡素化します。 config/app.php に設定されたファサードエイリアスからアクセスされます。

事前に構成されたフィード:

feedmanager は、事前に構築されたフィードを提供します:ユーザー、ニュース、および通知。 この例では、主にニュースと通知フィードを使用しています。 他のフィードタイプの詳細は、こちらで利用できます。

FeedManagerを使用して機能する/unfolow機能します:

the followcontroller

が更新され、 feedmanager を使用して、効率的なフォロー/フローアクション:

// app/http/controllers/followcontroller.php パブリック関数フォロー(ユーザー$ユーザー) { 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 follow'。$ user-> name); } それ以外 { return back() - > with( 'error'、 ''既にこのユーザーをフォローしています。 '); } } 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'、 'flolowed'。$ user-> name); } それ以外 { return back() - > with( 'error'、 'このユーザーに従わない'); } }
// 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

は、フィード検索と表示を処理するために作成されています:

getStream \ StreamLaravel \ redich; クラスFeedScontrollerはコントローラーを拡張します { // ...他のコード... パブリック機能ニュースフィード(リクエスト$ request) { $ feed = feedmanager :: getNewsFeeds($ request-> user() - > id)['timeline']; $ activity = $ feed-> getActivities(0、25)['results']; $ activity = $ this-> rench() - > richactivities($ activity); return View( 'feed.newsfeed'、['activity' => $ activity]); } プライベート関数redich() { 新しい濃縮を返します。 } // ...他の方法... }
// 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。内で作成されます。

created.blade.php

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