[PHP] MVC

搭建一个PHP的MVC项目,

主要思想是

Controller, 调用Model来获取数据,然后把获取到的数据交给View来显示。

        $user = $this->model('User');
        $user->name = $name;
        
        $this->view('home/index', array('name'=>$user->name));    //$this->view(controller/method, $data)

 

 

Model, 创建一个数据表对象,就跟Rails中的对象关系很像。在其中可以添加对这表操作的逻辑关系,CRUD.

        $user = $this->model('User');
        $user->name = $name;
        
        $this->view('home/index', array('name'=>$user->name));

 

View: 负责显示。

因为view中被Basecontroller的创建过程中被included进去了,所以,定义的$data,可以在View中使用:

Hello <?php echo $data['name'];?>

 

----------------------------------

app/core/App.php: parseURl, 设定controller, method, params

<?php
/**
 * Created by PhpStorm.
 * User: Answer1215
 * Date: 9/12/14
 * Time: 3:15 PM
 */

class App{

    protected $controller = 'home';
    protected $method = "index";
    protected $params = array();

    public function __construct(){
        $url = $this->parseUrl();
        
        if(file_exists('../app/controller/'. $url[0] .'.php')){
            $this->controller = $url[0];
            unset($url[0]);
        }

        require_once('../app/controller/'.$this->controller.'.php');

        $this->controller = new $this->controller;

        if(isset($url[1])){
            if(method_exists($this->controller, $url[1])){
                $this->method = $url[1];
                unset($url[1]);
            }
        }

        $this->params = $url ? array_values($url):array(); //rebase the index

        $ctrl_mth = array();
        array_push($ctrl_mth, $this->controller);
        array_push($ctrl_mth, $this->method);
        call_user_func_array($ctrl_mth, $this->params);
    }

    public function parseUrl(){

        if(isset($_GET['url'])){
            return $url = explode('/', filter_var(rtrim($_GET['url'], '/'),FILTER_SANITIZE_URL));
        }
    }
}

 

app/core/Controller.php: require php files and new model

<?php
/**
 * Created by PhpStorm.
 * User: Answer1215
 * Date: 9/12/14
 * Time: 3:17 PM
 */
class Controller{

    public function model($model){
        if(file_exists('../app/models/'. $model. '.php')){
            require_once('../app/models/'. $model. '.php');
            return new $model();    
        }else{
            echo "No ".$model." model";
            exit();
        }

    }
    
    
    public function view($view, $data=array()){
         require_once('../app/views/'.$view.'.php');
    }
}

 

app/modles/User.php

<?php

    class User{
        public $name;

        public function createNewUsers(){
            //Insert new users into users table
        }

        public function fetchAllUser(){
            // Get all users from the table
        }

        public function deletUser(){


        }

        public function updateUserInfo(){


        }
    }

 

app/controller/home.php:

<?php
/**
 * Created by PhpStorm.
 * User: Answer1215
 * Date: 9/12/14
 * Time: 3:38 PM
 */
class Home extends Controller{

    public function index($name = ""){

        $user = $this->model('User');
        $user->name = $name;
        
        $this->view('home/index', array('name'=>$user->name));
    }

}

 

app/views/home/index.php: controller, method

Hello <?php echo $data['name'];?>

 

app/.htaccess:

Options -Indexes

 

public/.htaccess:

Options -MultiViews
RewriteEngine on

RewriteBase /mymvc/public

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]

 

Read More:https://buckysroom.org/videos.php?cat=88

 

posted @ 2014-09-13 14:49  Zhentiw  阅读(329)  评论(0)    收藏  举报