tourze / user-follow-bundle
Symfony bundle for user follow/follower relationship management with event handling and EasyAdmin integration
Installs: 0
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
Type:symfony-bundle
pkg:composer/tourze/user-follow-bundle
Requires
- doctrine/dbal: ^4.0
- doctrine/doctrine-bundle: ^2.13
- doctrine/orm: ^3.0
- doctrine/persistence: ^4.1
- easycorp/easyadmin-bundle: ^4
- knplabs/knp-menu: ^3.7
- monolog/monolog: ^3.1
- psr/log: ^3|^2|^1
- symfony/config: ^7.3
- symfony/dependency-injection: ^7.3
- symfony/doctrine-bridge: ^7.3
- symfony/event-dispatcher: ^7.3
- symfony/event-dispatcher-contracts: ^3
- symfony/finder: ^7.3
- symfony/framework-bundle: ^7.3
- symfony/http-foundation: ^7.3
- symfony/http-kernel: ^7.3
- symfony/property-access: ^7.3
- symfony/security-core: ^7.3
- symfony/twig-bundle: ^7.3
- symfony/yaml: ^7.3
- tourze/bundle-dependency: 1.*
- tourze/doctrine-indexed-bundle: 1.0.*
- tourze/doctrine-snowflake-bundle: 1.1.*
- tourze/doctrine-timestamp-bundle: 1.1.*
- tourze/doctrine-user-bundle: 1.0.*
- tourze/easy-admin-menu-bundle: 1.0.*
- tourze/symfony-dependency-service-loader: 1.0.*
- tourze/user-service-contracts: 1.1.*
- twig/twig: ^3.21
Requires (Dev)
This package is auto-updated.
Last update: 2025-11-14 14:42:23 UTC
README
A Symfony bundle that provides user follow/follower relationship management functionality with comprehensive event handling and EasyAdmin integration.
Features
- User Follow System: Follow/unfollow users with status management
- Follow Statistics: Get followers count and following count
- Event System: Dispatch events when users follow/unfollow others
- EasyAdmin Integration: Built-in admin interface for managing follow relationships
- Doctrine Integration: Full database persistence with indexing
- Blameable & Timestampable: Track who created the relationship and when
Installation
composer require tourze/user-follow-bundle
Configuration
Add the bundle to your bundles.php:
return [ // ... Tourze\UserFollowBundle\UserFollowBundle::class => ['all' => true], ];
Basic Usage
Using the Follow Service
<?php use Tourze\UserFollowBundle\Service\FollowService; use Symfony\Component\Security\Core\User\UserInterface; class UserService { public function __construct( private FollowService $followService, ) {} public function checkFollowStatus(UserInterface $currentUser, UserInterface $targetUser): bool { return $this->followService->isFollowing($currentUser, $targetUser); } public function getUserStats(UserInterface $user): array { return [ 'followers' => $this->followService->getFansCount($user), 'following' => $this->followService->getFollowCount($user), ]; } }
Creating Follow Relations
<?php use Tourze\UserFollowBundle\Entity\FollowRelation; use Doctrine\ORM\EntityManagerInterface; class FollowManager { public function __construct( private EntityManagerInterface $em, ) {} public function followUser(UserInterface $follower, UserInterface $followed): void { $relation = new FollowRelation(); $relation->setUser($follower); $relation->setFollowUser($followed); $relation->setStatus(true); $this->em->persist($relation); $this->em->flush(); } public function unfollowUser(UserInterface $follower, UserInterface $followed): void { $relation = $this->em->getRepository(FollowRelation::class) ->findOneBy([ 'user' => $follower, 'followUser' => $followed, 'status' => true, ]); if ($relation) { $relation->setStatus(false); $this->em->flush(); } } }
Event Handling
Listen to follow events:
<?php use Tourze\UserFollowBundle\Event\AfterFollowUser; use Symfony\Component\EventDispatcher\Attribute\AsEventListener; class FollowNotificationListener { #[AsEventListener(event: AfterFollowUser::class)] public function onAfterFollowUser(AfterFollowUser $event): void { $follower = $event->getFollower(); $followedUser = $event->getFollowedUser(); // Send notification, update statistics, etc. // ... } }
Database Schema
The bundle creates a forum_follow_relation table with the following structure:
id: Snowflake ID (primary key)user_id: ID of the follower (foreign key)follow_user_id: ID of the followed user (foreign key)status: Boolean status (true = following, false = unfollowed)created_at: Timestamp when relation was createdupdated_at: Timestamp when relation was last updatedcreated_by: User who created the relation
EasyAdmin Integration
The bundle automatically registers admin controllers for managing follow relationships in the EasyAdmin backend.
API Reference
FollowService
-
isFollowing(UserInterface $follower, UserInterface $followedUser): bool- Check if a user is following another user
-
getFansCount(UserInterface $user): int- Get the number of followers for a user
-
getFollowCount(UserInterface $user): int- Get the number of users that a user is following
FollowRelation Entity
-
getUser(): ?UserInterface- Get the follower
-
setUser(?UserInterface $user): void- Set the follower
-
getFollowUser(): ?UserInterface- Get the followed user
-
setFollowUser(?UserInterface $followUser): void- Set the followed user
-
getStatus(): ?bool- Get the follow status
-
setStatus(?bool $status): void- Set the follow status
Events
AfterFollowUser: Dispatched after a user follows another usergetFollower(): Returns the user who initiated the followgetFollowedUser(): Returns the user who was followed
Dependencies
- Symfony 7.3+
- Doctrine ORM 3.0+
- EasyAdmin Bundle 4.0+
- Tourze Doctrine User Bundle
- Tourze Doctrine Snowflake Bundle
- Tourze Doctrine Timestamp Bundle
License
MIT License
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.