Some development experience on MVC in PHP_PHP Tutorial
After nearly a month of research on MVC, I have my own MVC process and framework through the guidance of friends online. However, I feel that there are still many limitations and a lack of flexibility, but I don’t know how to make specific improvements, so today I will publish my process and thoughts, hoping that someone with expertise can give me some advice.
1. Entrance
The entry file can be a single file or multiple files. The one I use now is basically multiple files, but the contents of the entry files are basically the same. This will serve as a basis for future modifications to other entry methods,
<?php require 'command/config.php'; require 'command/app.php'; app::run($config); ?>
Needless to say, everyone can see it first, load the system configuration file, and then load the system configuration through the engine.
2. Engine
public function run($config){ header("Content-type:text/html;charset=utf-8"); self::$config = $config; //加载系统配置 self::copyright(); self::testsystem(); //系统环境 self::setsystem(); //设置系统参数 self::incinfo(); if(!IN_WEB){exit('网站正关闭维护中,请稍候访问!');} defined('KEHENG_DEBUG') or define('KEHENG_DEBUG',true); // 是否调试模式 self::setpath(); //设置系统路径 self::getdatabase(); //测试数据库 self::loadlib(); //加载库 self::getRouteConfig(); //运行路由并加载控制器 }
In the engine, first set the configuration file, then test the system parameters, load the system module, obtain the configured website information file, set the path required by the website, test the database parameters in the system configuration, load the library file, and finally load the route acquisition Request address. I don’t know if this process is correct, it’s just a set I wrote based on my own learning, but it lacks cache. What should be the specific cache settings?
The database test here is based on which type of database is configured, and then the encapsulation file for the operation of this type of database is loaded.
3. Routing
The following is the last function above, which loads the controller file and obtains the request method according to the configuration file.
public function getRouteConfig(){ $route_type=self::$config[route][url_type]; switch($route_type){ case 1: //echo $_SERVER['SCRIPT_NAME'].'<br />'; $query_string=$_SERVER['QUERY_STRING']; //echo $_SERVER['REQUEST_URI'].'<br />'; $urlstr=$_GET['controller']; break; case 4: $url = end(explode('/', $_SERVER["PHP_SELF"])); $urlstr = strtolower(substr($url,0,-4)); break; } if(file_exists(Contr_DIR.'Controller.php')){ require Contr_DIR.'Controller.php'; //echo $urlstr; $template = self::$config['Templates']; controller::load($urlstr,$template); }else{ exit('控制器文件不存在'); } }
4. Controller
The controller file is also quite simple. It just loads the model file and view file based on the address analyzed by the route,
class controller{ public $obj; public function load($url,$template){ $config=$template; if(file_exists(Model_DIR.$url.'.model.php')){ $views = new views; //echo Model_DIR.$url.'.model.php'; require Model_DIR.$url.'.model.php'; $temp = $config[$url][0]; if($temp!='' && $temp!=null && isset($temp)){ if(file_exists(Templ_DIR.$temp)){ //echo Templ_DIR.$temp; require Templ_DIR.$temp; }else{ exit('视图文件不存在!'.$temp); } }else{ exit('此页未设置显示模板!'.$temp); } unset($views); }else{ exit('模型文件不存在:'.$url.'.model.php'); } } }
But one thing to note is that all the data that needs to be output in the model file is output through a class such as views, including all system parameters in the view file in the package. I don’t know if this method is unnecessary. It turns out that the purpose is to encapsulate all the data to be output.
Other template files are also encapsulated with classes. Experts should know how to write them specifically. These are just my personal opinions, but how to write cache is still a vague concept. Is it reading data? When , the direction should be to read the cache, then determine whether the cache exists, and then determine whether the cache needs to be established? The specific operation method is still not very clear. I hope someone can give me some advice.
Articles you may be interested in
- The simplest way to implement MVC development in PHP, thinking about the model
- Share an article made by a foreign webmaster Traffic experience
- PHP smarty Chinese interception plug-in development example
- php function to calculate how many years, months, and days between two dates
- uft- in php and mysql 8 Several solutions to garbled Chinese encoding
- php searches whether a certain value exists in an array (in_array(), array_search(), array_key_exists())
- php calculates the difference between two dates How many days (days) function
- Tips for using MVC pattern in php

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
