search
HomeBackend DevelopmentPHP TutorialMVC framework PHP creates its own MVC framework

1. File structure
Create 3 folders
The controller folder stores controller files
The view folder stores view files
The model folder stores data files
Create 1 index.php as the only entrance
2. Controller
We are here Create a democontroller.php file under the controller folder. The file content is as follows

Copy the code The code is as follows:


class DemoController
{
function index()
{
echo('hello world ');
}
}
/* End of file democontroller.php */


In this file we just created an object named DemoController and contains an index method, which outputs hello world. Next, execute the index method in DemoController in index.php. The code of
index.php is as follows

Copy the codeThe code is as follows:


require('controller/democontroller.php');
$c DemoController();
$controller-> index();
/* End of file index.php */


Run index.php, ok as expected we saw our long-lost hello world. These two files are very simple, but they also reveal a little bit of the essence of MVC, running the controller we want to run through the only entrance. Of course, the controller part should be determined by the uri, so let's rewrite index.php so that it can determine which controller to run through the uri.
index.php rewrite the code as follows:

Copy the code The code is as follows:


$c_str=$_GET['c'];
//Get the controller to run
$c_name= $c_str.'Controller';
//According to the agreement, the controller name obtained from the URL does not contain Controller, so fill it in here.
$c_path='controller/'.$c_name.'.php';
//According to the agreement, the controller file must be created in the controller folder, the class name must be the same as the file name, and the file name must be all lowercase.
$method=$_GET['a'];
//Get the action to be run
require($c_path);
//Load the controller file
$c $c_name;
//Instantiate the controller file
$controller- >$method();
//Run the action under this instance
/* End of file index.php */


Enter http://localhost/index.php?c=demo&a=index in the browser , got our hello world. Of course, if we have another controller and want to run it, we only need to modify the values ​​of c and a in the url parameters.
There are a few questions to explain here.
1. PHP is a dynamic language. We can directly get the object we want and run the method we want through the string new, that is, the new $c_name above, we can understand it as new 'DemoController', because $c_name itself The value is 'DemoController'. Of course, writing new 'DemoController' directly will not work. The 'DemoController' string must be transferred through a variable. The method is the same.
2. The value of c in our URL is demo, which means the value of $c_name should be demoController. Isn’t PHP case-sensitive? Can it run like this? The sentence "php is case-sensitive" is incomplete. In php, only variables (preceded by $) and constants (defined by define) are case-sensitive, while class names, method names and even some keywords are not case-sensitive. written. And true, false, null, etc. can only be all uppercase or all lowercase. Of course we'd better be case-sensitive during the actual encoding process.
3. View
We only output a "hello world" in the previous controller, which did not achieve the effect of mvc. Next, I will add the view function on this basis. I believe that by now everyone can basically think of how to add the view function. . Yes, it is achieved through the evil require or include.
First we create an index.php in the view folder and write anything (haha, I still wrote hello world). Then we rewrite our previous DemoController. The code is as follows:

Copy the code The code is as follows:


class DemoController
{
function index()
{
require('view/index.php');
}
}
/* End of file democontroller.php */


Run it in the browser again to see if the content we want has been output.
Then let’s pass some data to the view through the controller. The code is as follows:

Copy the code The code is as follows:


class DemoController
{
function index()
{
$data[ 'title']='First Title';
$data['list']=array('A','B','C','D');
require('view/index.php');
}
}
/* End of file democontroller.php */


The index.php file code in the view folder is as follows:

Copy the code The code is as follows:




demo



foreach ($data['list'] as $item)
{
echo $item.'
';
}
?>

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
php的mvc框架有哪些php的mvc框架有哪些Jul 24, 2023 am 10:52 AM

php的mvc框架有:1、Laravel,具有简单、优雅和可扩展的语法,提供了丰富的功能和强大的开发工具;2、Symfony,以灵活性和可扩展性而闻名,提供了许多组件和工具;3、CodeIgniter,简单而快速的MVC框架,拥有清晰简洁的代码和轻量级的体量,适用于快速开发小型和中型的Web应用程序;4、Yii,高性能的MVC框架,注重安全性和可扩展性等等。

PHP中的MVC框架有哪些?PHP中的MVC框架有哪些?May 12, 2023 pm 09:40 PM

随着互联网技术的发展,MVC框架成为了Web开发中最受欢迎的一种思想和模式。其中,PHP语言作为一种Web开发语言,也有着丰富的MVC框架。本篇文章将介绍一些常用的PHPMVC框架。一、LaravelLaravel是目前PHP中最受欢迎的MVC框架之一,也是一个开放源代码的PHPWeb框架,由TaylorOtwell创建。Laravel采用了现代的PH

php开源mvc框架有哪些php开源mvc框架有哪些Aug 23, 2023 pm 01:26 PM

php开源mvc框架有Laravel、Symfony、CodeIgniter、Yii和Phalcon等。详细介绍:1、Laravel是一个流行的PHP框架,它提供了简洁优雅的语法和丰富的功能,它具有强大的路由系统、数据库抽象层、队列处理、缓存管理和认证功能等,Laravel还提供了一个活跃的社区和广泛的文档资源,使得学习和使用变得更加容易;2、Symfony等等。

php mvc有哪些php mvc有哪些Aug 01, 2023 pm 05:29 PM

php mvc有Laravel、Symfony、CodeIgniter和Yii。1、Laravel,提供了丰富的功能和工具,用于快速开发高效的Web应用程序;2、Symfony,提供可复用的组件和模块;3、CodeIgniter,提供简单而强大的开发工具和功能;4、Yii,提供了丰富的功能和灵活的扩展性。

Go语言的MVC框架开发详解Go语言的MVC框架开发详解Jun 03, 2023 am 10:02 AM

随着互联网技术的发展和全球化的趋势,越来越多的开发者选择使用Go语言进行开发,而MVC框架是一种被广泛应用的Web框架。本文将详细介绍Go语言中MVC框架的开发,旨在帮助开发者更好地理解和运用MVC框架。一、MVC框架简介MVC(Model-View-Controller)是一种软件开发中的架构模式,它将一个应用程序分为三个核心部分:模型(Model)、视图

PHP7.0中的MVC框架有哪些?PHP7.0中的MVC框架有哪些?May 27, 2023 pm 04:51 PM

PHP7.0中的MVC框架有哪些?随着互联网应用的高速发展,越来越多的网站和企业应用选择了采用PHP编程语言开发,而MVC(Model-View-Controller)架构已成为PHP开发中常用的架构模式。MVC的基本思想是将应用程序分为三个模块:模型(Model)、视图(View)和控制器(Controller),提高程序的可维护性和可扩展性。在PHP7.

php有哪些mvc框架php有哪些mvc框架Aug 02, 2023 pm 01:31 PM

php的mvc框架有:1、Laravel,功能强大的MVC框架,有活跃的社区,提供大量的文档和教程;2、Symfony,稳定强大的MVC框架,提供了高度可定制的组件和Bundle的概念;3、CodeIgniter,简单灵活的MVC框架,具有小巧的体积和快速的执行速度;4、Yii,高性能的MVC框架,提供丰富的特性;5、Phalcon,高性能的MVC框架;6、CakePHP等等。

php中mvc框架有哪些php中mvc框架有哪些Aug 23, 2023 am 11:25 AM

php中mvc框架有Laravel、Symfony、CodeIgniter、Yii、Phalcon、CakePHP和Zend Framework等。详细介绍:1、Laravel是目前最受欢迎的PHP框架之一,提供了很多有用的功能和工具,如路由、ORM、数据库迁移、模板引擎等,Laravel具有简洁的语法和优雅的设计,使得开发人员可以快速构建高性能的Web应用程序等等。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MinGW - Minimalist GNU for Windows

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.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools