alexandrebulete / ddd-blog-bundle
Blogging as a reusable DDD Bounded Context: posts in Markdown, categories, tags, scheduled publication, Sylius admin.
Package info
github.com/AlexandreBulete/ddd-blog-bundle
Type:symfony-bundle
pkg:composer/alexandrebulete/ddd-blog-bundle
Requires
- php: ^8.4
- alexandrebulete/ddd-doctrine-bridge: ^1.4
- alexandrebulete/ddd-foundation: ^1.6
- alexandrebulete/ddd-sylius-bundle: ^1.2
- alexandrebulete/ddd-symfony-bundle: ^1.6
- doctrine/dbal: ^4.3
- doctrine/doctrine-bundle: ^2.13 || ^3.0
- doctrine/doctrine-migrations-bundle: ^3.7 || ^4.0
- doctrine/migrations: ^3.8
- doctrine/orm: ^3.3
- league/commonmark: ^2.6
- pagerfanta/core: ^4.0
- sylius/admin-ui: ^0.14
- sylius/grid-bundle: ^1.13
- sylius/resource-bundle: ^1.14
- symfony/clock: ^7.0 || ^8.0
- symfony/config: ^7.0 || ^8.0
- symfony/dependency-injection: ^7.0 || ^8.0
- symfony/form: ^7.0 || ^8.0
- symfony/framework-bundle: ^7.0 || ^8.0
- symfony/http-foundation: ^7.0 || ^8.0
- symfony/security-core: ^7.3 || ^8.0
- symfony/string: ^7.0 || ^8.0
- symfony/translation: ^7.0 || ^8.0
- symfony/uid: ^7.0 || ^8.0
- symfony/validator: ^7.0 || ^8.0
- twig/twig: ^3.10
- webmozart/assert: ^1.11
Requires (Dev)
- alexandrebulete/ddd-media-bundle: ^1.1
- deptrac/deptrac: ^4.7
- phpstan/phpstan: ^2.1
- phpstan/phpstan-strict-rules: ^2.0
- phpunit/phpunit: ^12.0 || ^13.0
Suggests
- alexandrebulete/ddd-media-bundle: Post visuals: a picture heading each post, kept in a media library
Provides
None
Conflicts
None
Replaces
None
README
Blogging as a reusable DDD Bounded Context for Symfony: posts written in Markdown, filed under categories and tags, published now or scheduled, with a Sylius back office.
The bundle owns the use cases, their persistence, their schema and the back office. The public side — routes, templates, URLs — belongs to the application, which asks the public queries and prints the result.
Installation
composer require alexandrebulete/ddd-blog-bundle
The Flex recipe registers the bundle. Its tables (blog_*) come with its own
migrations: bin/console doctrine:migrations:migrate, nothing to diff.
Requires ddd-foundation, ddd-symfony-bundle, ddd-doctrine-bundle and ddd-sylius-bundle.
Model
| Aggregate | Holds |
|---|---|
Post |
title, slug, excerpt, Markdown body, author id, status, publication date, featured, category ids, tag ids, visual id |
Category |
name, slug, description — flat, no hierarchy |
Tag |
name, slug — created the first time a post is tagged with it |
- Several categories per post, in the order given: the first is the main one, for a theme that shows a single category.
- Scheduling is a date, not a job. A post published with a date ahead is
scheduled; it becomes visible when the date passes, with nothing to run.
Public reads always ask "visible now?" (
Post::isVisibleAt()). - Author is an identifier from the application's identity context (an IAM user id), with no foreign key: the blog does not know who users are.
- Visual is an identifier of a picture kept elsewhere (a media library), with no foreign key either.
- A published post cannot be emptied or deleted (unpublish or archive first); a category or tag still carried by posts cannot be removed.
Use cases
Commands: DraftPost, RetitlePost, ChangePostSlug, RewritePost,
CategorizePost, TagPost, PublishPost (now or publishAt),
UnpublishPost, ArchivePost, FeaturePost, UnfeaturePost,
ChangePostVisual, DeletePost;
CreateCategory, RenameCategory, DescribeCategory, ChangeCategorySlug,
RemoveCategory; RenameTag, RemoveTag.
Back-office queries: FindPosts, FindPost, FindCategories,
FindCategory, FindTags, FindTag.
Public queries (#[PublicAccess], open to any visitor, signed in or not):
| Query | For |
|---|---|
FindPublishedPosts |
a listing: all, by category, by tag, featured — paginated, most recent first |
FindPublishedPost |
one post by slug; a draft or a scheduled post is not found |
FindRelatedPosts |
"read also": sharing the most categories and tags, topped up with the latest |
FindPublishedCategories |
the categories holding visible posts, with their count, by name: a category menu that never leads to an empty page |
FindCategoryBySlug, FindTagBySlug |
resolve a URL segment (404 when unknown) |
FindCategoriesByIds, FindTagsByIds |
the categories and tags of a post, in its order |
Every other use case is a permission (blog.draft_post, blog.find_posts…),
granted through roles by an IAM.
Rendering
{{ post.body|blog_markdown }}
{% for entry in blog_toc(post.body) %}
<a href="#{{ entry.anchor }}" class="toc-{{ entry.level }}">{{ entry.title }}</a>
{% endfor %}
{{ post.readingTime }} min
blog_markdown is CommonMark + GitHub Flavored Markdown. Raw HTML is escaped
and unsafe links dropped: an author writes Markdown, not HTML, and the output
prints as is. h2/h3 get ids, which blog_toc() links to.
Visuals
With alexandrebulete/ddd-media-bundle installed, the post form gets a picture field and the grid a thumbnail; the picture goes to the media library, and the post keeps its id. Without it, posts have no picture field — everything else is the same.
{% set visual = media(post.visual) %}
{% if visual %}
<img src="{{ media_url(visual, 'post_banner') }}" alt="{{ visual.alt }}">
{% endif %}
post_banner stands for any LiipImagine filter set the application
declares. A picture sent from a post's form belongs to that post: replacing
it, taking it off or deleting the post takes it out of the library, unless
something else shows it. The library refuses to delete a picture still
heading a post.
Configuration
blog: admin: enabled: true # Sylius screens; false for a headless setup grid_limits: [20, 50, 100]
Seams
Ports bound in config/services.php, to rebind in the application:
| Port | Default | Rebind for |
|---|---|---|
DomainEventPublisherInterface |
ImmediateEventPublisher (in-process, in the transaction) |
an outbox, for delivery outside the process |
SlugGeneratorInterface |
AsciiSlugGenerator (Symfony slugger) |
slugs keeping Unicode |
PostRepositoryInterface… |
Doctrine | another storage |
Persistence
A post keeps its category and tag ids in its own row (JSON), read and written
whole with it. Two index tables, blog_post_category and blog_post_tag,
are rewritten from it on every save, in the same transaction: filtering
by category or tag is an indexed EXISTS, portable to PostgreSQL, MySQL and
SQLite.
Development
composer qa # phpstan (max, strict) + deptrac + phpunit
Integration tests run on SQLite in memory, or on the database given by
DDD_TEST_DATABASE_URL (CI: PostgreSQL, MySQL, SQLite).
License
MIT