2

I recently picked up learning PHP and MySQL and I am trying to build a small usersystem as a coding exercise. While learning to use classes and functions, I also stumbled upon the MVC model and would like to implement the model in my code.

Is the following approach correct MVC?

  • index.php, message.php, user.php are all a bunch of controllers. They start a model, collect $_GET, $_POST and $_SESSION user data and passes that on to the model. They may collect info from the model and pass it on to the views.

  • the user class, message class, etc. ... are all parts of the model. They validate the data that the controller gives them, do the actual database stuff and return data to the controller and the view.

  • the views are puzzled together by the controller. They mainly echo data from the model & the controller and include html files.

My main concern is how the view displays data from the model. Is it better to go through the controller or should I directly get it from the method whenever I can?

  • I advise starting with one of the major PHP MVC frameworks (e.g. CakePHP 3), which will give you some guidance, and start you off understanding the workings of MVC, with extensive documentation to aid your learning as well. If you are trying to reinvent the wheel by building your own MVC framework you might find you learn a lot less, as you don't know what you don't know yet, and spend a lot of time trying to find the answer to questions which may become very clear when you see it done already. – BadHorsie Jun 22 '16 at 15:30
2

Your understanding is correct. Keep in mind that different frameworks use different approaches, so something which would for instance apply to Laravel won't necessarily apply to, say, Symfony.

This is especially true when it comes to deciding where the logic goes: some frameworks use models as basic objects containing data, but practically no logic by themselves. Other frameworks encourage you to move some logic from the controllers to the models.

A few comments:

  • Why is your controller called index.php? Usually, the application has several controllers with the names which indicate their purpose. For instance, an e-commerce website can have a products.php file which contains the controller logic handling http://example.com/products/... requests, customers.php file for customer-related stuff, cart.php for you guess what, etc.

    Unless you have separate directories for each of them, such as /products/index.php, customers/index.php and cart/index.php, you are putting all the logic in the same file. As a consequence, even if the file is currently small, it will grow through time, and become very difficult to maintain.

  • While doing input validation inside a controller is a perfectly valid approach, some frameworks move the validation logic from the controller to (1) the model and (2) the framework itself. I don't know PHP frameworks, so I'll give an example of ASP.NET MVC: usually, properties within the models have specific attributes which indicate how should they be validated. Then, basic validation logic is applied by the framework itself during the mapping.

  • I'm not using any framework. In my previous attempts I put the validation logic in the model, but that resulted in long elseif statements right above the actual data processing. Is that a valid reason to put the validation logic into the controller? Also, when I would add messaging functionality, you mean that the view should display the amount of unread messages from a string passed on from the controller but the view cannot do an if/else to see if there are any unread messages? – Max Jun 22 '16 at 15:42
  • "but the view cannot do an if/else": ah, that checking! I thought you were talking about checking whether the model is valid. So yes, basic if/else statements, such as in your example of unread messages, have their place in the view. That's why most (if not all) template engines have conditional statements. – Arseni Mourzenko Jun 22 '16 at 15:50
  • 1
    "I'm not using any framework." Why not? Don't you think an MVC framework would make your life much easier? Without one, you'll end up reinventing things such as routing or a template engine. Have you looked at Laravel? I'm sure you'll find it useful, and it will also help you understanding MVC better. – Arseni Mourzenko Jun 22 '16 at 15:54
  • Building one myself and ending up reinventing things seemed like a fun thing to do. Sorry for not following your advice on this one. I consider if(empty($_POST[""])) to be a part of the controller, before handing the variable to the model. For easier to find code, I would group the validation logic with the if(empty()), because I consider if(empty()) to be a form of validation. Is that correct or am I missing something? – Max Jun 22 '16 at 16:18
  • After some more reading, I decided to put the validation logic in the model. So now in my approach, the controller does the routing of the user data to the model, then the model validates the data and performs the action or not, then the controller checks the model to see if the action was performed or not and uses this to construct the view. – Max Jun 23 '16 at 17:46
-1

One of the benefits of using Visual Studio for Asp.NET MVC is the seperation. When I develop something using CakePHP, it feels like everything gets jumbled into a few single files and the code will get clunky. If you are still going down the road of MVC (which I highly recommend), you might want to check out DJango or even Asp.NET MVC with the community edition of Visual Studio. DJango even has its own ORM built in to handle all of your database needs if you don't mind switching over to Python.

  • I am aware of the benefits of existing frameworks, but I'm trying to create a (small and simple) MVC framework myself as an exercise to get a better understanding of PHP. – Max Jun 23 '16 at 14:07
  • Question is php tagged. If anything you should suggest Laravel. I don't get how ASP.NET MVC comes into the equation. – Alternatex Jun 24 '16 at 7:19
  • Actually, its tagged as PHP with another tag of MVC. I was referring to another MVC framework. I also mentioned Python/Django, so really I think I was pointing out other potential MVC frameworks. Op explained later how he/she wanted to code their own from scratch. That is the part that I missed originally. – Kevin B Burns Jun 25 '16 at 2:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.