Home Backend Development PHP Tutorial PHP creates its own MVC framework_PHP tutorial

PHP creates its own MVC framework_PHP tutorial

Jul 21, 2016 pm 03:19 PM
controller mvc php v one store belong Establish build controller document folder frame structure my own

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

Copy 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 code The code is as follows:

require( 'controller/democontroller.php');
$controller=new 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 be run
$c_name=$c_str.'Controller';
//According to the agreement, the controller name obtained from the URL does not contain Controller. Complete 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 run
require($c_path);
//Load the controller file
$controller=new $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 and get 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 several issues 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 The value itself 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 everyone can basically think of it by now. How to add view function. Yes, it is achieved through the evil require or include.
First we create an index.php under the view folder and write anything (haha, I still wrote hello world). Then we rewrite our previous DemoController. The code is as follows:
Copy 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 under the view folder is as follows:
Copy code The code is as follows:



demo


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


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/325267.htmlTechArticle1. Create 3 folders in the file structure. The controller folder stores the controller files. The view folder stores the view files and model files. Create an index.php folder to store data files as the only entrance...
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)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
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)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

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

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

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

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

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

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

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

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

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

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

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

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

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

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

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

See all articles