有关PHP中MVC的开发经验分享_php技巧
入口文件可以是单文件也可以是多文件,我现在用的基本属多文件,但是入口文件内容基本都是一样,为以后的修改其它的入口方式做基础,
require 'command/config.php';
require 'command/app.php';
app::run($config);
?>
首先不用说大家也看得出来,加载系统配置文件,然后通过引擎来加载系统配置。
二、引擎
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(); //运行路由并加载控制器
}
引擎里面首先设置配置文件,再测试系统参数,加载系统模块,取得配置在的网站信息文件,设置网站需要的路径,测试系统配置里面的数据库参数,加载库文件,最后是加载路由获取请求地址。不知道这样的流程对不对,只是我根据自己的学习自己编写的一套而已,但里面却缺少缓存,具体缓存应该怎么样的设置。
这里的数据库测试是根据配置用哪一类型的数据库,再加载对该类型数据库操作的封装文件。
三、路由
以下为上面的最后一个函数,加载控制器文件,根据配置文件获得请求方式。
public function getRouteConfig(){
$route_type=self::$config[route][url_type];
switch($route_type){
case 1:
//echo $_SERVER['SCRIPT_NAME'].'
';
$query_string=$_SERVER['QUERY_STRING'];
//echo $_SERVER['REQUEST_URI'].'
';
$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('控制器文件不存在');
}
}
四、控制器
控制器文件也蛮简单,只是根据路由分析出的地址来加载模型文件和视图文件,
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');
}
}
}
但里面有个注意的是模型文件里面需要输出的数据全部都是通过views这样一个类进行输出,包里视图文件里面所有的系统参数等。不知道,这种方法是不是显示得多此一举,原来是想把所有要输出的数据进行封装。
其它的模板文件也都是用类进行了封装,具体怎么写高人应该都知道了吧,这些只是我的个人见解,但是缓存应该怎么写,现在还是一个模糊的概念,是不是在读取数据的时候,方向应该是读取缓存,然后再判断缓存是否存在,再判断是否需要建立缓存呢?具体操作方法还是不是很明白。希望能有高人指点指点。

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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











JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

In PHP8, match expressions are a new control structure that returns different results based on the value of the expression. 1) It is similar to a switch statement, but returns a value instead of an execution statement block. 2) The match expression is strictly compared (===), which improves security. 3) It avoids possible break omissions in switch statements and enhances the simplicity and readability of the code.
