2012-08-16 16:24
浏览 66

php MVC概念,rails

I want to make clear something before i proceed with my project to prevent bad code practice. I developed in ruby on rail before and i want to know is it can make it the same like in PHP because im starting learning but i don't want to use the framework like Zend,Yii,etc

For me, model is the place where data is insert to the database.

controller is the place that find all the parameters and use the function in model to process the data into database. Besides, it also route or redirect when the procedure is return success or failure.

view is the place that get the instant variable that initialize in controller ,and use it to render the data in proper format.

So,here the question for me in PHP. I would like to create CRUD in php.I walked through many of the article or posts(in stackoverflow& others),but i got confused that some of the people saying different kind of things.

In my mind, model is the place that only related to data insertion/query to database. So,every query for my create,show,update,delete, i will put in my model. here is my example for create_user function in my model.

function create_user($firstName, $lastName,$username, $password){
    $query = "INSERT INTO `users` (`user_id`, `first_name`, `last_name`, `email`, `username`, `password`, `created_at`, `handphone_no`, `street_address`, `city`, `state`, `country`, `postal`, `birthday`, `company_id`)
            VALUES (NULL, ?, ?, NULL, ?, ?, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 2)";

    if($stmt = $this->conn->prepare($query)) {
        $stmt->bind_param('ssss', $firstName, $lastName,$username, $password);


So,my FIRST question: in my model, am i suppose to an object and pass to controller?? or just return true or false to indicate the success or failure of the process.

in my user controller,

class Users_controller {

private $user;

function __construct(){
    $this->user = New User();

function create($firstName, $lastName,$username, $password){
    $this->user->create_user($firstName, $lastName,$username, $password);

my SECOND question: for my CRUD, in rails, it will either find the id of the object and show/edit it or create/destroy a new model. So, my controller here have to return an object for me to render data in view? and how am i suppose to connect my form to the controller. From my form to pass the params to the controller.

so,right now Im thinking so far of that only, any guidance and clear example for me to understand more because Im new to php. Thanks you...

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • doulu6929 2012-08-16 17:34

    Okay, hopefully this answer won't be too long, but here we go...

    Firstly, RoR is in fact a framework, so the closest thing you'll get to that in PHP would be a framework. However, you will not find a rails clone (or anything close) in PHP. Also, nearly every PHP framework out there claims to implement MVC, but almost none of them do. Most of them present some sort of modified & simplified version of MVC with rampant SOLID violations and all sorts of other issues. Within this answer I'll refer to these variations of MVC as "FMVC" for short. With all that being said, proper MVC is technically impossible in PHP due to the necessity of persistent models. (note: you can do this in PHP, but it's not easy and will likely be some ugly/"hacky" code).

    Here's my recommendation, if you're up for some learning: If you have a project in mind, pick a framework that generally appeals to you, and go with it. Learn it inside and out, dive into the source code, get some of your code reviewed, and be sure to (mostly) ignore all of the nay-sayers of that particular framework. I say "mostly" because if you really want to learn MVC or OOP-PHP in general, the best way to do that is to look at existing implementations and truly understand the good, the bad, and the ugly parts of the code. What this will give you is a solid (excuse the pun) foundation of knowledge in SOLID principles, as well as SOLID violations.

    If you're looking to write your own MVC framework or learn MVC in general, there's a few things to understand first:

    1. In the OP it seems like you're misunderstanding what exactly the model is; In the MVC pattern the "M" (model) is a layer. If you'd like a long-winded but extremely accurate description of a model, see this post by tereško, as it's currently the best such answer on SO. In short the model layer is made up of many parts, namely Data Mappers, Domain Objects, and "Services." Data mappers are similar to "model" classes in FMVC - they contain the database queries. Domain Objects is basically just a container for domain data. "Services" are the point of interaction with the model layer: you call the service to do something, and it does the work within the model layer. Generally you won't be directly calling data mappers (or domain objects) outside of services.

    2. CRUD is not a thing, it's a concept - a list of methods to include in the class (create, read, update, delete). Data Mappers generally contain CRUD methods (and by extension so do services, although sometimes a bit more abstract)

    3. To reiterate (because it's very important), almost no PHP frameworks implement proper MVC, so looking to them for guidance on learning MVC is a bad idea. You might learn FMVC this way, but proper MVC is a long ways off from what they implement.

    4. If you don't fully understand OOP and SOLID principles, coding in MVC will be a nightmare. Going back to and re-learning or refreshing on the basics of OOP is the first thing you should do if you want to learn MVC

    Now for your specific questions:

    1. This question doesn't really make sense. Calls to the model layer are made through services, which generally pass a domain object to the relevant data mapper for processing. The data mapper returns the domain object (which contains all of the result data) back to the service, which was called from outside the model layer.

    2. This question also makes no sense - reading the post that I linked earlier should clear up some of the confusion.

    tl;dr - read this post.

    打赏 评论
  • douyu4535 2012-08-16 16:50

    I'm not sure why you wouldn't want to use a PHP framework, since RoR is also a framework.

    I've developed my own PHP framework before and can tell you from experience it is generally a bad idea unless you have some very specific requirements for your application.

    I would recommend you try Yii, it's quite liberal in the coding it allows.

    Anyway, to answer your questions :

    First I would create a generic model class that does the CRUD stuff rather than having to recreate for every class. So for example you define the properties available (or get directly from DB) and have a generic "save" function that would do an insert or update based on the presence of the value(s) for the primary key.

    Then when you save, you should return true on success, false on invalid data/validation, and throw an exception if the SQL request fails.

    Passing data from the form to the controller is done via POST, and the values are injected into the object. From there you can check if the object exists in the DB or not.


    打赏 评论

相关推荐 更多相似问题