doukesou4452 2010-05-17 17:45
浏览 30
已采纳

过渡到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)

  • 写回答

5条回答 默认 最新

  • dongpenggan6812 2010-05-17 18:37
    关注

    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" );
    }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大