Home Backend Development PHP Tutorial MVC framework PHP creates its own MVC framework

MVC framework PHP creates its own MVC framework

Jul 29, 2016 am 08:48 AM
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:



< head>
demo



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


The above has introduced the mvc framework. PHP can create its own MVC framework, including the content of the mvc framework. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement of this Website
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

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)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the mvc frameworks for php What are the mvc frameworks for php Jul 24, 2023 am 10:52 AM

PHP's mvc frameworks include: 1. Laravel, which has a simple, elegant and scalable syntax, providing rich functions and powerful development tools; 2. Symfony, known for its flexibility and scalability, provides many components and Tools; 3. CodeIgniter, a simple and fast MVC framework with clear and concise code and lightweight size, suitable for rapid development of small and medium-sized Web applications; 4. Yii, a high-performance MVC framework that focuses on security performance and scalability, etc.

What are the PHP open source MVC frameworks? What are the PHP open source MVC frameworks? Aug 23, 2023 pm 01:26 PM

PHP open source mvc frameworks include Laravel, Symfony, CodeIgniter, Yii and Phalcon, etc. Detailed introduction: 1. Laravel is a popular PHP framework. It provides simple and elegant syntax and rich functions. It has a powerful routing system, database abstraction layer, queue processing, cache management and authentication functions. Laravel also provides An active community and extensive documentation resources make learning and using easier; 2. Symfony and more.

What are the MVC frameworks in PHP? What are the MVC frameworks in PHP? May 12, 2023 pm 09:40 PM

With the development of Internet technology, the MVC framework has become the most popular idea and model in Web development. Among them, PHP language, as a Web development language, also has a rich MVC framework. This article will introduce some commonly used PHPMVC frameworks. 1. Laravel Laravel is currently one of the most popular MVC frameworks in PHP and an open source PHPWeb framework created by Taylor Otwell. Laravel adopts modern PH

What are php mvc What are php mvc Aug 01, 2023 pm 05:29 PM

PHP mvc includes Laravel, Symfony, CodeIgniter and Yii. 1. Laravel, which provides a wealth of functions and tools for quickly developing efficient web applications; 2. Symfony, which provides reusable components and modules; 3. CodeIgniter, which provides simple and powerful development tools and functions; 4 , Yii, provides rich functions and flexible scalability.

Detailed explanation of MVC framework development in Go language Detailed explanation of MVC framework development in Go language Jun 03, 2023 am 10:02 AM

With the development of Internet technology and the trend of globalization, more and more developers choose to use Go language for development, and the MVC framework is a widely used Web framework. This article will introduce in detail the development of the MVC framework in the Go language, aiming to help developers better understand and use the MVC framework. 1. Introduction to MVC framework MVC (Model-View-Controller) is an architectural pattern in software development. It divides an application into three core parts: Model and View.

What are the mvc frameworks for php? What are the mvc frameworks for php? Aug 02, 2023 pm 01:31 PM

PHP's mvc frameworks include: 1. Laravel, a powerful MVC framework with an active community that provides a large number of documents and tutorials; 2. Symfony, a stable and powerful MVC framework that provides highly customizable components and Bundle concepts; 3. CodeIgniter, a simple and flexible MVC framework with small size and fast execution speed; 4. Yii, a high-performance MVC framework that provides rich features; 5. Phalcon, a high-performance MVC framework; 6. CakePHP, etc. .

What are the mvc frameworks in php What are the mvc frameworks in php Aug 23, 2023 am 11:25 AM

MVC frameworks in PHP include Laravel, Symfony, CodeIgniter, Yii, Phalcon, CakePHP and Zend Framework, etc. Detailed introduction: 1. Laravel is one of the most popular PHP frameworks at present. It provides many useful functions and tools, such as routing, ORM, database migration, template engine, etc. Laravel has concise syntax and elegant design, making development People can quickly build high-performance web applications and more.

What are the MVC frameworks in PHP7.0? What are the MVC frameworks in PHP7.0? May 27, 2023 pm 04:51 PM

What are the MVC frameworks in PHP7.0? With the rapid development of Internet applications, more and more websites and enterprise applications choose to use the PHP programming language to develop, and the MVC (Model-View-Controller) architecture has become a commonly used architectural pattern in PHP development. The basic idea of ​​MVC is to divide the application into three modules: Model, View and Controller to improve the maintainability and scalability of the program. In PHP7.

See all articles