stijnhau / forum-bundle
Forum (categories, topics, posts, moderation) and its admin for Symfony apps.
Package info
github.com/stijnhau/forum-bundle
Type:symfony-bundle
pkg:composer/stijnhau/forum-bundle
Requires
- php: >=8.4
- doctrine/doctrine-bundle: ^3.4
- doctrine/orm: ^3.8
- psr/event-dispatcher: ^1.0
- symfony/config: ^8.1
- symfony/dependency-injection: ^8.1
- symfony/framework-bundle: ^8.1
- symfony/html-sanitizer: ^8.1
- symfony/http-kernel: ^8.1
- symfony/translation-contracts: ^3.5
Requires (Dev)
- phpunit/phpunit: ^13.4
- symfony/browser-kit: ^8.1
- symfony/css-selector: ^8.1
- symfony/event-dispatcher: ^8.1
- symfony/form: ^8.1
- symfony/security-bundle: ^8.1
- symfony/translation: ^8.1
- symfony/twig-bundle: ^8.1
- symfony/yaml: ^8.1
Suggests
- symfony/form: For the ready-made server-rendered forum and admin pages (stijnhau_forum.ui)
- symfony/security-bundle: For the ready-made server-rendered forum and admin pages (stijnhau_forum.ui)
- symfony/twig-bundle: For the ready-made server-rendered forum and admin pages (stijnhau_forum.ui)
Provides
None
Conflicts
None
Replaces
None
README
The forum and its admin, shared by CityWars, Crime-Life and F1Manager: categories, topics, posts, moderation (edit/remove with history, delete), and category management.
What's in the box:
| Piece | What it does |
|---|---|
Model\Abstract* |
Doctrine mapped superclasses for category, topic and post. The app maps a concrete subclass of each, so table names stay the app's and it can add columns (an owning alliance, say). |
Service\Forum |
Start a topic, reply, edit/remove a post (moderators), delete a topic or reply. Every rule lives here; refusals are ForumExceptions. |
Service\CategoryAdmin |
The admin: list, create, edit, reorder and delete (only if empty) the public categories. |
Query\ForumReader |
The reads: public categories with topic counts, a category's topics a page at a time, a topic's posts a page at a time. |
Service\ContentPolicy |
The title/body/category checks plus sanitizing, for an app's own private sub-forums to reuse. |
| Twig UI (optional) | Server-rendered forum and admin pages (ui.enabled: true). |
JSON/SPA apps keep their own thin controllers and shape the JSON themselves,
on top of Forum, CategoryAdmin and ForumReader.
Install
composer require stijnhau/forum-bundle
Register Stijnhau\ForumBundle\StijnhauForumBundle::class => ['all' => true] in config/bundles.php.
Wire it up
-
The user entity implements
Stijnhau\ForumBundle\Model\ForumUserInterface(getId(),getForumDisplayName(),isForumModerator()). -
Map the three entities:
#[ORM\Entity] class ForumCategory extends AbstractForumCategory {} #[ORM\Entity] class ForumTopic extends AbstractForumTopic {} #[ORM\Entity] class ForumPost extends AbstractForumPost {}
-
Configure
config/packages/stijnhau_forum.yaml:stijnhau_forum: user_class: App\Entity\User category_class: App\Entity\ForumCategory topic_class: App\Entity\ForumTopic post_class: App\Entity\ForumPost per_page: 20 # posts/topics per page body_max_length: 4000 # sanitized HTML length authors_may_delete: false # may authors delete their own topics/replies? category_description_required: true ui: enabled: false # the Twig pages below admin_role: ROLE_ADMIN
-
bin/console make:migration. The bundle ships no migrations; the schema belongs to the app.
Hooks
-
Who may post: implement
Security\PostingGuardInterface(mutes, bans, rate limits). Every implementation is picked up automatically and runs before each new topic and reply. Throw to refuse. -
Private sub-forums: if the category table also holds private categories (an alliance's own, say), implement
Scope\ForumScopeInterfaceand alias it:services: Stijnhau\ForumBundle\Scope\ForumScopeInterface: '@App\Forum\PublicForumScope'
Public lookups, listings and every
Forum/CategoryAdminaction then treat those categories as missing. -
Events:
TopicCreatedEvent,PostCreatedEvent,PostEditedEvent,PostRemovedEvent,TopicDeletedEventandPostDeletedEvent, each carrying the actor, topic and post.isByAuthor()tells moderation apart from self-service, for a moderation log, say. -
Errors:
ForumException::getKey()is a key in thestijnhau_forumtranslation domain (en/nl/fr ship with the bundle).$e->trans($translator)gives the text.
Twig UI
With ui.enabled: true, import the routes:
# config/routes/stijnhau_forum.yaml stijnhau_forum: resource: '@StijnhauForumBundle/config/routes.php' prefix: /forum trailing_slash_on_root: false stijnhau_forum_admin: resource: '@StijnhauForumBundle/config/routes_admin.php' prefix: /admin/forum trailing_slash_on_root: false
Every page requires a logged-in ForumUserInterface; the admin pages also
require ui.admin_role. To plug the pages into your layout, override
templates/bundles/StijnhauForumBundle/forum/layout.html.twig (keep the
forum_title and forum blocks) and .../admin/layout.html.twig (keep
forum_title and forum_admin). You can override any other template the
same way. The body field carries data-forum-editor for hooking up a rich-text
editor. Without JavaScript, plain text is turned into paragraphs.
Development
composer install vendor/bin/phpunit
License
MIT, see LICENSE.