[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
分类:
PHP
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 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工具