Complete Installation first. This example adds a comment form to an existing post page using the plugin's separate controller. Your application already needs Posts, Users, and a PostsController::view() action.
The key is the public URL alias. The value is the table class name to load. Only configured aliases can receive comments through the plugin controller.
Anonymous comments are disabled by default. A signed-in request must have an identity whose identifier matches a record in Users. See Access and identity if your application needs guest comments or uses a legacy session login.
The plugin uses your application's AppController, layout, Flash component, authentication, and middleware. Permit the public CommentsController::add() action for the users who may comment, while retaining CSRF and form protection. Render flash messages in your layout if it does not already do so:
Open an existing post while signed in, submit a nonempty comment, and reload the page. The new comment should appear on that post. Submission redirects to the referring page; without a local referer it falls back to the application home page.
An empty comment is rejected with an error flash message. A signed-out submission receives a 403 unless anonymous comments are enabled. Escape comment content when rendering it, as the template above does.
The plugin stores is_private and is_spam flags, but these flags do not automatically hide comments in this basic query. Add your visibility policy to the query before using moderation flags in production. See Models and migrations.
If you prefer posting to the same application action, use the Comment component instead.
Getting started
Complete Installation first. This example adds a comment form to an existing post page using the plugin's separate controller. Your application already needs
Posts,Users, and aPostsController::view()action.Allow comments on posts
Add this configuration to the array returned by
config/app.php:The key is the public URL alias. The value is the table class name to load. Only configured aliases can receive comments through the plugin controller.
Anonymous comments are disabled by default. A signed-in request must have an identity whose identifier matches a record in
Users. See Access and identity if your application needs guest comments or uses a legacy session login.Load the helper
In
src/View/AppView.php, load the helper insideinitialize()alongside your existing helpers:Display the comments
Attach the behavior in
src/Model/Table/PostsTable.php, inside its existinginitialize()method:In your existing
PostsController::view()action, load the post and its comments:Add this to
templates/Posts/view.php:The plugin uses your application's
AppController, layout, Flash component, authentication, and middleware. Permit the publicCommentsController::add()action for the users who may comment, while retaining CSRF and form protection. Render flash messages in your layout if it does not already do so:Check the result
Open an existing post while signed in, submit a nonempty comment, and reload the page. The new comment should appear on that post. Submission redirects to the referring page; without a local referer it falls back to the application home page.
An empty comment is rejected with an error flash message. A signed-out submission receives a 403 unless anonymous comments are enabled. Escape comment content when rendering it, as the template above does.
The plugin stores
is_privateandis_spamflags, but these flags do not automatically hide comments in this basic query. Add your visibility policy to the query before using moderation flags in production. See Models and migrations.If you prefer posting to the same application action, use the Comment component instead.