donlih2986 2015-01-08 01:59
浏览 56
已采纳

PHP MVC - 这是正确的结构吗? [关闭]

I have some problems understanding mvc in php.

My folder structure looks like this:

controller
|- member.php
model
|- member.php
view
|- overview.php

My code looks like this:

controller > member.php

include '../model/member.php';

class Member{
    public function create_table_of_all_members(){
        $model = new Member();

        $members = $model->get_all_members();

        foreach ($members as $member) {
            echo $member;
        }
    }

    public function create_form(){

    }
}

model > member.php

class Member{
    public function __construct()
     {
        // DB-Connection initialization
     }

    function getAllMembers(){
        $sql = "";
        return $result;
    }
}

view > overview.php

include '../controller/member.php';

$controller = new Member();
$controller->create_table_of_all_members();

Am I on the right way? In the view folder I would put e.g overview (all pages with a table), form (all pages with a form)..

  • 写回答

1条回答 默认 最新

  • dte8665 2015-01-08 02:34
    关注

    It's kind of hard to define what is right or wrong at conceptual discussions such as this one. All I can provide you is my own limited experience and opinion. Absorb it with your own opinion and decides what's best for you in your own opinion. Build your first architecture, implement your first project and then your own project will start to tell you if your architecture is working for you or not. Here is a few personal tips.


    • I never output anything to the browser from any layer except the VIEW.

    That is, if you're moving towards MVC model, try to keep the connection between the server and the response to a single layer, which seems to be a perfect job for the VIEW layer, that handles anything from and to what the user is seing (User Interface).


    • Learn addicional design pattern to complete your archtecture

    In my opinion, MVC is not a fully working concept/archtecture. That is, it perfectly defines key responsabilities, but doesn't define how to make everything work. For instance, how will you define your model? You can go with Active Record (like your code represents) or you can opt out for DAO (Data Access Object). In a few words, the differences would be like this:

    class ActiveRecordModel {
        protected function connect();
        protected function create();
        protected function update();
        protected function delete();
    }
    
    class Car extends ActiveRecordModel {
        public $id;
        public $maker;
        public $model;
        public $year;
    }
    

    This way, you can use $myCar->create(); and the model will be created inside the database. As oppose to DAO

    class Car {
        private $id;
        private $maker;
        private $model;
        private $year;
    
        // Getters, Setters, Constructor
    }
    
    class CarDAO {
        public function add(Car $obj){
             // Establish connection here or at the constructor
             // Take data from $obj and arrange it to be stored.
        }
    }
    

    • Controllers talk to Controllers, but only one Model

    I usually prefer making one controller for each model and that controller will know how to handle that model (regardless of which design pattern the model is using). That being said, I avoid using more than 1 model inside one controller. If you need to access data from a model that is not your own, talk to it's controller instead of bypassing the controller and going direct to the model. Here is what I mean:

    class AccountController extends GenericController {
         public function add($name, $phone, $birthday, $email, $password) {
              $accountModel = new Account();
              $accountModel->name = $name;
              $accountModel->phone = $phone;
              $accountModel->birthday = $birthday;
              $accountModel->password = $password;
              $id = $accountModel->insert();
    
              $emailController = new EmailController();
              $emailController->add($email, $id);
              // Here, instead of declaring a model for "Email",
              // I'm declaring the controller
              // so, in my architecture, whenever I need to change some rule
              // for some entity, I know I only have to work at one single controller.
         }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助