PHP MVC Structure

2019-08-31 01:34发布

问题:

As part of a university project I'm developing a web application in PHP and trying to follow MVC principles as much as possible. I'm not using an off-the-shelf framework because I want as much of the project to be my work as possible to help get a higher mark.

To delete an item (each item has an id, status, title, parent_id) from a database I go do the following process....

  • User hits a delete button and is asked to confirm
  • When clicking confirmed a "page" loads which calls the controller, initiates a model which is stored in the controller object
  • A method in the model object is called to load the item from the database based on the ID in the URL
  • I edit a status property in the model to mark the item as deleted
  • I then save the model to the database which in effect deletes it.

I also want to delete all child items in the database of the item that has just been deleted. Where is the best place to do this? In the the controller? In a separate functions file? In a model file?

I'm pretty new to the whole MVC idea, so any help / advice is much appreciated.

回答1:

The basic breakdown of MVC is

  • Model: Business logic
  • View: Presentation logic
  • Controller: Glue logic (triggers model functionality based on user input, render views based on model output).

The statement "Deleting an item will cause all its children to also be deleted" is a description of something that needs to happen in the business logic. Therefore, it belongs in the model.

Implementation is up to how you have implemented getting child items and deleting items in your model, but it will typically consist of getting a collection of your model's children in its delete method, and invoking the delete method for each child in the collection (each child will also look for children and delete them when this happens). Now the view and controller don't have to worry about whether or not a model has children, as it's all taken care of in the model's business logic.



回答2:

Part of being a good developer is knowing when not to reinvent the wheel. And this is certainly one of those situations. I suggest you first ask your professor(s) whether you are allowed to use an off-the-shelf framework or not. This "I want as much of the project to be my work" is pretty much nonsense. If you really want it to be your own work, do it in assembler :).

If you still have to write everything from scratch (in PHP), I suggest you first look at well-established MVC frameworks (e.g. Symfony), in order to get the feel of how such a framework should work. Maybe have a look at the source code, too.



回答3:

I would probably do that outside the PHP MVC pattern. Using MySQL innoDB tables you can achieve that using Foreign Keys:

CREATE TABLE parent (id INT NOT NULL,
                     PRIMARY KEY (id)
) ENGINE=INNODB;
CREATE TABLE child (id INT, parent_id INT,
                    INDEX par_ind (parent_id),
                    FOREIGN KEY (parent_id) REFERENCES parent(id)
                      ON DELETE CASCADE
) ENGINE=INNODB;