过渡到MVC编码的最佳方法是什么?

It's been around 5 months since I picked up a PHP book and started coding in PHP. At first, I created all my sites without any organizational plan or MVC. I soon found out that was a pain.. Then I started to read on stackoverflow on how to separate php and html and that's what I have been doing ever since.

Ex: 
profile.php <--this file is HTML,css. I just echo the functions here. 
profile_functions.php <--this file is mostly PHP. has the functions.

This is how I have been separating all my coding so far and now I feel I should move on and start MVC. But the problem is, I never used classes before and suck with them. And since MVC (such as cakephp and codeigniter) is all classes, that can't be good.

My question: Is there any good books/sites/articles that teaches you how to code in MVC? I am looking for beginner beginner books :) I just started reading the codeigniter manuel and I think I am going to use that.

EDIT: Is it possible to have a MVC organization structure to your coding without using cake, codeigniter, etc? Basically just separate say profile.php into 3 different files(the view, controller, model)

duanqiang2977
duanqiang2977 当然,可以在没有框架的情况下以MVC方式编写......但是框架将教会您最佳实践,并且您不必编写使其全部工作的所有管道代码。
10 年多之前 回复

5个回答

to answer your question

Is it possible to have a MVC organization structure to your coding without using cake, codeigniter, etc? Basically just separate say profile.php into 3 different files(the view, controller, model)

absolutely...

first file profile.php ( the view, what gets hit by the browser )

<?php
include( 'controllers/UsersController.php' );
$controller = new UsersController();
$controller->profile();
$pageData = $controller->data;
?>

the controller

<?php
include 'models/UsersModel.php';
class UsersController{

public $data;
public $model;

public function __construct(){
    $this->model = new UserModel();
}

public function profile(){
    $this->data = $this->model->findUser();
}

}

the model

<?php

class UsersModel{

public function __constuct(){
    // connect to your db or whatever you need to do
}

public function findUser(){
    return mysql_query( "SELECT * FROM users WHERE users.id =  2  LIMIT 1" );
}
}



这不是PHP,但看看你是否可以获得Tate Bitter Java的副本。 它将讨论事物的组织方面(组织代码改进的方式和原因)。</ p>

我有点犹豫是否推荐一本用于PHP编程的优秀Java书籍,但是这个 本书是少数几个没有组织计划编写的代码之一,并且在没有使用第三方库的情况下将其改进为类似MVC的结构。 这样,它从实际的角度教你组织的内容。 希望一旦你理解了这个模式,将这些想法转化为PHP就不会太难了。</ p>

另一个选择是抓住几十个PHP框架中的一个并重新编码到框架中。 这样做可以更快地获得结果,但缺点是您可能会更详细地了解这些结果,并且在您从头开始重写代码后,代码的行为很可能不会相同。 我们都喜欢认为新的东西会做旧的东西所做的一切,但往往我们会忘记一些东西(或者它的行为有所不同)。</ p>
</ div>

展开原文

原文

It's not PHP, but see if you can get a copy of Tate's Bitter Java. It will discuss the organizational side of things (how and why the organizational code improves stuff).

I'm a bit hesitant to recommend one of the great Java books for PHP programming, but this book is one of the few that starts with code written without an organizational plan and improves it into a MVC like structure without the use of 3rd party libraries. This way it teaches you what the organization is about from a practical point of view. Hopefully once you understand the pattern, it won't be too difficult to translate the ideas into PHP.

Another alternative is to grab one of the dozens of PHP frameworks and recode to the framework. Doing so will get your results much faster, but the drawback is that you'll likely understand those results in greater detail, and there is a small chance your code will not behave the same after you rewrite it from scratch. We all like to think the new stuff will do everything the old stuff did, but often we forget something (or it behaves differently).

MVC is a "generic" design pattern that is not particular to any language. More of a coding philosophy. On the most basic level it's just separating data from business logic from presentation. Below is a simple example of a "templating" system using MVC. You would be able to swap out any of the parts without breaking anything, and the data is not tied to the formatting/display. This is sample code, not efficient.

Model, get the data:

function getName($id) {
    $name = array('_first_'=>'Joe', '_last_'=>'Smith', '_mi_'=>'C');
    return $name
}

Controller, processes it:

$name = getName(1);
$name['_fullname_'] = $name['_first_'].' '.$name['_mi_'].'. '.$name['_last_'];
outputView($name);

View, output the content:

// Example html file: <b>Hello _fullname_, how are you</b>
function outputView($view, $data) {
    switch ($view) {
    case 'xml':
         $out = file_get_contents('view.xml');
    case 'html':
         $out = file_get_contents('view.html');
    case 'json':
         $out = file_get_contents('view.json');
    }
    $search_for = array_keys($data);
    $replace_with = $data;
    echo str_replace($search_for, $replace_with, $out);
}



MVC只是一种设计模式。 这不是你可以“编写”的东西。</ p>

如果你想用PHP编写代码,这里有一个文章。 它概述了设计模式,然后是一个例子。</ p>
</ div>

展开原文

原文

MVC is just a design pattern. It's not really something you can "code in".

If you like to code in PHP, here is an article regarding MVC in PHP. It has an overview explaining the design pattern, and then an example follows.



我是如何通过本教程学到的:

http://www.symfony-project.org/jobeet/1_4/Doctrine/en/ </ p>

主要关注的是学习Symfony框架,但默认情况下,您将接触并学习良好的MVC原则。 </ p>
</ div>

展开原文

原文

How I learned was by going through this tutorial:
http://www.symfony-project.org/jobeet/1_4/Doctrine/en/

The primary focus is to learn the Symfony Framework, but by default, you will be exposed to and learn good MVC principles.

dtcd27183
dtcd27183 是的 - 但更好 - 带上它;)
10 年多之前 回复
doutuo7609
doutuo7609 Symfony是另一个像codeigniter一样的php框架吗?
10 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问