简体   繁体   English

PHP MVC概念,rails

[英]php MVC concept, 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 我之前在铁路上开发过Ruby,我想知道它可以使它像PHP中一样,因为即时消息开始学习,但是我不想使用Zend,Yii等框架。

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. 所以,这里的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. 我想在php中创建CRUD。我浏览了许多文章或帖子(在stackoverflow等文章中),但我感到困惑的是,有些人在说不同的话。

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. 这是我模型中的create_user函数示例。

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. 或仅返回true或false来指示该过程的成功或失败。

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. 我的第二个问题:对于我的CRUD,在rails中,它将找到对象的ID并进行显示/编辑,或者创建/销毁新模型。 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. 所以,现在我只是想到目前为止,任何指导和明确的例子让我了解更多,因为我是php新手。 Thanks you... 谢谢...

I'm not sure why you wouldn't want to use a PHP framework, since RoR is also a framework. 我不确定为什么您不想使用PHP框架,因为RoR也是一个框架。

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. 我以前已经开发了自己的PHP框架,并且可以从经验中告诉您,除非您对应用程序有一些非常具体的要求,否则通常这不是一个好主意。

I would recommend you try Yii, it's quite liberal in the coding it allows. 我建议您尝试使用Yii,它在允许的编码方面相当宽松。

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. 首先,我将创建一个执行CRUD的通用模型类,而不必为每个类都重新创建。 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. 因此,例如,您定义了可用的属性(或直接从DB获取),并具有一个通用的“保存”功能,该功能将根据主键的值的存在来进行插入或更新。

Then when you save, you should return true on success, false on invalid data/validation, and throw an exception if the SQL request fails. 然后,在保存时,如果成功,则应返回true,如果数据/验证无效,则应返回false,如果SQL请求失败,则将引发异常。

Passing data from the form to the controller is done via POST, and the values are injected into the object. 通过POST将数据从表单传递到控制器,然后将值注入到对象中。 From there you can check if the object exists in the DB or not. 从那里可以检查对象是否存在于数据库中。

HTH 高温超导

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. 首先,RoR实际上是一个框架,因此与PHP最接近的是框架。 However, you will not find a rails clone (or anything close) in PHP. 但是,您不会在PHP中找到rails克隆(或任何接近的东西)。 Also, nearly every PHP framework out there claims to implement MVC, but almost none of them do. 另外,几乎所有的PHP框架都声称要实现MVC,但几乎没有一个实现。 Most of them present some sort of modified & simplified version of MVC with rampant SOLID violations and all sorts of other issues. 他们中的大多数人都提出了某种MVC的修改和简化版本,其中普遍存在违反SOLID的行为以及其他各种问题。 Within this answer I'll refer to these variations of MVC as "FMVC" for short. 在此答案中,我将把MVC的这些变体简称为“ FMVC” With all that being said, proper MVC is technically impossible in PHP due to the necessity of persistent models. 话虽如此,由于持久性模型的必要性,在PHP中,正确的MVC在技术上是不可能的。 (note: you can do this in PHP, but it's not easy and will likely be some ugly/"hacky" code). (请注意:您可以在PHP中执行此操作,但这并不容易,并且可能会是一些难看/“ hacky”的代码)。

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. 我之所以说“主要”是因为,如果您真的想一般地学习MVC或OOP-PHP,最好的方法是查看现有的实现,并真正理解代码的好,坏和丑陋的部分。 What this will give you is a solid (excuse the pun) foundation of knowledge in SOLID principles, as well as SOLID violations. 这将为您提供SOLID原则以及SOLID违规知识的坚实基础(不好意思)。

If you're looking to write your own MVC framework or learn MVC in general, there's a few things to understand first: 如果您想编写自己的MVC框架或学习一般的MVC,首先需要了解以下几点:

  1. In the OP it seems like you're misunderstanding what exactly the model is; 在OP中,您似乎误解了模型的确切含义。 In the MVC pattern the "M" (model) is a layer . 在MVC模式中,“ M”(模型)是一层 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. 如果您想要一个冗长而又极其准确的模型描述,请参阅tereško的这篇文章 ,因为它目前是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. 数据映射器类似于FMVC中的“模型”类-它们包含数据库查询。 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). CRUD不是一回事,而是一个概念-包含在类中的方法列表(创建,读取,更新,删除)。 Data Mappers generally contain CRUD methods (and by extension so do services, although sometimes a bit more abstract) 数据映射器通常包含CRUD方法(通过扩展,服务也包含CRUD方法,尽管有时更抽象一些)

  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. 重申一下(因为它非常重要),几乎没有PHP框架实现正确的MVC,因此,向他们寻求学习MVC的指导是一个坏主意。 You might learn FMVC this way, but proper MVC is a long ways off from what they implement. 您可能会以这种方式学习FMVC,但是正确的MVC与实现它们之间还有很长的路要走。

  4. If you don't fully understand OOP and SOLID principles, coding in MVC will be a nightmare. 如果您不完全了解OOP和SOLID原理,那么使用MVC进行编码将是一场噩梦。 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 如果要学习MVC,首先应该重新学习OOP的基础知识或对其进行更新。

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 . tl; dr-阅读此文章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM