[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 @   Zhentiw  阅读(323)  评论(0编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示