php - 过渡到 MVC 编码的最佳方式是什么?

标签 php model-view-controller frameworks

自从我拿起一本 PHP 书籍并开始使用 PHP 编码已经过去了大约 5 个月。起初,我在没有任何组织计划或 MVC 的情况下创建了我所有的网站。我很快发现那很痛苦.. 然后我开始在 stackoverflow 上阅读有关如何分离 php 和 html 的内容,这就是我从那以后一直在做的事情。

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.

到目前为止,这就是我分离所有编码的方式,现在我觉得我应该继续并开始 MVC。但问题是,我以前从未使用过类并且很讨厌它们。而且由于 MVC(例如 cakephp 和 codeigniter)都是类,所以这不太好。

我的问题:有什么好书/网站/文章可以教您如何使用 MVC 编码吗?我正在寻找初学者入门书籍 :) 我刚开始阅读 the codeigniter manuel我想我会使用它。

编辑:是否可以在不使用 cake、codeigniter 等的情况下将 MVC 组织结构用于您的编码?基本上只是将 profile.php 分成 3 个不同的文件( View 、 Controller 、模型)

最佳答案

回答你的问题

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)

绝对...

第一个文件 profile.php( View ,浏览器访问的内容)

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

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();
}

}

模型

<?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 - 过渡到 MVC 编码的最佳方式是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2851386/

相关文章:

php - 无法使用 gmail SMTP 在 Zend 中发送电子邮件。收到 fatal error : Uncaught exception 'Zend_Mail_Protocol_Exception' . 如何解决?

asp.net-mvc-3 - 使用 Html.TextBoxFor 时,将模型名称作为前缀添加到 id

objective-c - iOS 音乐应用程序的音频框架

iOS 自制框架可以包含其他第三方框架吗?

php - 什么是 PHP 框架?

php - 在 CodeIgniter 中使用 Classname() 或 __construct() 作为构造函数?

php - Zend_Test_PHPUnit_Constraint_DomQuery::evaluate() 的声明应该与 PHPUnit_Framework_Constraint::evaluate() 的声明兼容

php - 在 Ubuntu 上升级 PHP 版本

node.js - Sails.js API 文档

model-view-controller - 现有的营养成分数据库?