Comment API
This module provides components and other necessary traits required by other modules / models to start posting a comment.
Adding comment to a model
- Any model that needs to have ability to add in a comment should implement the
HasCommentsInterfacecontract and use theHasCommentsconcern.
<?php
namespace Modules\__\Models;
use Modules\CommentApi\Concerns\HasComments;
use Modules\CommentApi\Contracts\HasCommentsInterface;
class Model extends BaseModel implements HasCommentsInterface
{
use HasComments;
}
- The model should then define a getCommentableComponentActionUrls method that defines the action urls for the models comment.
/**
* {@inheritdoc}
*/
public function getCommentableComponentActionUrls(?Comment $comment = null): array
{
$baseCommentRouteName = SurveyRouteService::SUBMISSION_RELATIONSHIP.'.comment.';
$baseParams = [SurveyRouteService::SUBMISSION_RESOURCE => $this->getFrontendKey()];
$actionUrls = [
'store' => route($baseCommentRouteName.'store', $baseParams, false),
];
if (! $comment) {
return $actionUrls;
}
$params = array_merge($baseParams, ['comment' => $comment->getFrontendKey()]);
return array_merge($actionUrls, [
'update' => route($baseCommentRouteName.'update', $params, false),
'delete' => route($baseCommentRouteName.'delete', $params, false),
'reply' => route($baseCommentRouteName.'reply', $params, false),
]);
}
- Now the module should define appropriate routes and controllers for comment actions. The controller should extend
FrontendCommentControllerprovided by this module.
<?php
namespace Modules\Survey\Http\Controllers\Frontend;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Modules\CommentApi\Http\Controllers\Frontend\FrontendCommentController;
use Modules\CommentApi\Models\Comment;
use Modules\Survey\Models\Submission;
class FrontendSubmissionCommentController extends FrontendCommentController
{
// Define actions here.
}
- Now the model can access the required commentable component props required for the frontend component via the
getCommentableComponentAttribute.
$model->commentable_component;
- A global commentable component is also registered via this module which can be used after model has been setup.
<component
v-if="model.commentable_component"
:is="model.commentable_component.component"
v-bind="model.commentable_component.props"
/>
Adding author to a comment model
To add a author to a comment model, the model in this case Modules\Users\Models\User, implements HasAuthorComments and uses HasAuthorCommentsInterface.
Nesting
The comment module is configured to handle multiple level of nesting. See config('comment.max_comment_depth'). Please use wisely as it may cause too excessive queries. The default nesting is set to 3, but is completely customizable via ( available in HasComments trait ):
/**
* {@inheritdoc}
*/
public function getCommentableComponentCommentsDepth(): int
{
return CommentService::getCommentsDepth();
}