第一篇文章已经把所需的目录搭建好了,接下来的工作就是写一些代码了
用编辑器打开public/index.php文件,写上下面的代码
<?php define(DS, DIRECTORY_SEPARATOR); define(ROOT, dirname(dirname(__FILE__))); $url = $_GET['url']; // 加载引导 require_once( ROOT . DS . 'core' . DS . 'bootstrap.php' );
入口文件的代码很简洁,其中$url是个全局变量,用来获取当做请求的参数
现在先把加载引导的那句注释掉,然后在下面echo $url,即
//require_once( ROOT . DS . 'core' . DS . 'bootstrap.php' );echo $url;
然后在浏览器地址栏输入http://localhost/wudicsmvc/
注:D:\AppServ\www\wudicsmvc
然后,你会发现,啥也没显示。
接着输入http://localhost/wudicsmvc/wudics
就会发现网页打出wudics这几个字
说明可以正常获取url参数了,这个作为一个全局变量存在,主要用来获取客户端的请求
如访问哪个控制器的哪个方法
http://localhost/wudicsmvc/home/index
可以这样规定,访问home控制器的index方法,当然你也可以有不同的规定,也就是路由规则,据说是mvc一个很重要的概念
index.php文件没有啥内容,加载了一个bootstrap.php文件,打开core目录下的bootstrap.php文件:
<?php // 加载配置 require_once(ROOT . DS . 'cfg' . DS . 'config.php'); // 路由请求 require_once(ROOT . DS . 'core' . DS . 'route.php');
同样加载了两个文件,一个是cfg目录下的config.php,一个是core目录下的route.php
看下config.php
<?php // mysql连接参数 define('DB_HOST', 'localhost'); define('DB_USER', 'root'); define('DB_PASS', '123456'); define('DB_NAME', 'transdb'); // smarty的一些常量 define('DIR_TPL', ROOT . DS . 'app' . DS . 'view' . DS . 'template_dir' . DS . 'default' . DS); define('DIR_CPL', ROOT . DS . 'app' . DS . 'view' . DS . 'compile_dir' . DS); define('DIR_CFG', ROOT . DS . 'app' . DS . 'view' . DS . 'config_dir' . DS); define('DIR_CAC', ROOT . DS . 'app' . DS . 'view' . DS . 'cache_dir' . DS); // 默认控制类方法 $default['controller'] = 'home'; $default['action'] = 'index'; // pulibc文件夹的路径,方便模板设计 define('WEBSITE', 'http://localhost'); define('WEBIMG', WEBSITE . dirname(dirname($_SERVER['PHP_SELF'])) . '/public/img/');
一些常量和全局变量,这个很有用,因为常量和全局变量在单一入口php程序里都是共享的资源
然后是route.php文件
<?php function callHook() { global $url; global $default; // Get the $controller $action $param $param = array(); $urlArr = explode("/", rtrim($url, "/")); $controller = array_shift($urlArr); $action = array_shift($urlArr); $param = $urlArr; if ($controller == "") { $controller = $default['controller']; $action = $default['action']; } if ($action == "") { $action = $default['action']; } // 控制类书写规则 HomeController->Index $controllerName = ucfirst($controller).'Controller'; $dispatch = new $controllerName($controller, $action); if (method_exists($dispatch, ucfirst($action))) { call_user_func_array(array($dispatch, ucfirst($action)), $param); } else { /* Error Code for Method is not exists */ die('method not exitsts.<br>'); } } // 自动加载类 function __autoload($classname) { if (file_exists(ROOT . DS . 'core' . DS . strtolower($classname) . '.class.php')) { require_once(ROOT . DS . 'core' . DS . strtolower($classname) . '.class.php'); } else if (file_exists(ROOT . DS . 'app' . DS . 'controller' . DS . strtolower($classname) . '.php')) { require_once(ROOT . DS . 'app' . DS . 'controller' . DS . strtolower($classname) . '.php'); } else if (file_exists(ROOT . DS . 'app' . DS . 'model' . DS . strtolower($classname) . '.php')) { require_once(ROOT . DS . 'app' . DS . 'model' . DS . strtolower($classname) . '.php'); } else { /* Error Code for can not find the files */ die('class not found.<br>'); } } callHook();
这个页面,首先定义了两个函数callHook和__autoload,然后页面底部有一句callHook();到此,程序就开始执行了。
callHook函数:
其作用就是定位上面所说的控制器,方法,参数
当代码执行这里时
// 控制类书写规则 HomeController->Index $controllerName = ucfirst($controller).'Controller'; $dispatch = new $controllerName($controller, $action);
程序就会执行__autoload方法,这个方法可以在你使用类的时候可以自动加载所需的文件
不需要手动require,这样可以方便的时候各种类库,方便省事,不需要每次调用类之前都手动加载.php类文件
method_exists方法判断这个类中是不是存在该方法,若存在,就call_user_func_array调用该方法
注:__autoload方法是一些加载类库文件的规则,这个要根据你的习惯来定义的。
至此,程序就跳到某某控制器的某某方法执行了。比如说HomeController类中的Index方法,该文件就在你的__autoload规则里定义着。
我这里的就是app/controller/homecontroller.php文件了
<?phpclass HomeController extends Controller{ public function Index() { $user = new user(); $tpl = new tpl(); $tpl->set('name', $user->name); $tpl->render('index.html'); }}
这个方法里加载了user类(Model)和tpl类(view)
user类负责从数据库读数据
tpl类负责把显示数据
所以也可以这样说,controller类起到了一个连接的作用,控制着数据和显示,使两者可以很好的绑定在一起,model和view就分开了
这样做就可以实现了当初的想法,把php代码和html布局分离开来
我期待我能够整合出一个属于自己的方便使用的php框架,希望大家来QQ群给点意见,QQ群号:667110936671109366711093

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.

In PHP, use the clone keyword to create a copy of the object and customize the cloning behavior through the \_\_clone magic method. 1. Use the clone keyword to make a shallow copy, cloning the object's properties but not the object's properties. 2. The \_\_clone method can deeply copy nested objects to avoid shallow copying problems. 3. Pay attention to avoid circular references and performance problems in cloning, and optimize cloning operations to improve efficiency.

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.


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

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Dreamweaver Mac version
Visual web development tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.