douju5062 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);
        $stmt->execute();
    }

} 

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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 matlab实现基于主成分变换的图像融合。
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊