C
C
counterprowtf2018-11-28 13:07:27
PHP
counterprowtf, 2018-11-28 13:07:27

How to implement comments on an OOP website using MVC?

I need to implement comments on an OOP site using MVC, do I need to create a model and a comment controller separately?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dmitry, 2018-11-28
@dimoff66

Well, MVC assumes this - creating a separate model, a separate view and a separate controller. Otherwise it won't be MVC.

I
Ivan Koryukov, 2018-11-28
@MadridianFox

Traditionally, comments on websites refer to something. For example, to an article, news, file, etc.
This means that comments are usually found on the page with the commented object. Those. comments are not a separate section or even an independent page, but only part of the page.
On the other hand, MVC is not even a design pattern, but something more - it is a way to organize the structure of a program. Usually the ENTIRE program.
Thus, to make only part of the site using MVC ... what's the site, part of the page! not very possible, because traditionally, one page is one controller (Page Controller, which is often expressed by a method of a certain class, also called a controller).
You can still create a comment model. You can even nicely separate the comment view. But here's a separate comment controller... that's unlikely.
But in fact, it all depends on the framework used and on the technologies with which you will make these comments.
Suddenly the site is written in Symfony, and you want to load comments via ajax? Then, of course, a separate controller can be done purely for comments.

N
Nikolino, 2018-11-30
@Nikolino

They do it differently. For each entity, you can create your own controller, or you can create something generic, like BlogController, and add methods for working with posts and comments there. Then the methods will be called something like this: addComment(), storeComment(), deleteComment(), addPost(), deletePost(), etc.
There are fewer controllers, but each one is "fatter" which can make things a bit more difficult.
If you create separate controllers, for example: CommentController, CategoryController, PostController, etc., then the methods usually do not include the name of the entity, so as not to be repeated. That is, we call the methods like this: store(), update(), delete(). To save, update and delete, respectively.
For example, comments are not published by default, and you can change their status to "published" from the admin panel.
Accordingly, the path in the router is called, which calls the update method of the comment controller, updates the status to "published" in the database, and redirects to where you need it, for example, to the list of all unpublished comments in the admin panel.
And when adding, say, a post, we turn through the router to the add () method of the post controller, which simply displays the post form, but the form itself, when submitting, already refers to the store () method of the same post controller.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question