Home  >  Article  >  php教程  >  Handwrite my own simple MVC framework myPHP

Handwrite my own simple MVC framework myPHP

WBOY
WBOYOriginal
2016-08-08 08:49:491847browse

myPHPFramework

It adopts the MVC idea, applies pure object-oriented and single entry to the project, and implements a customized framework. (Practice of your own interest)

1. Single entrance to the project

Entry file

myphpindex.php front desk

请 All requests on a website request a file (entry file) index.phpadmin.php. The entrance is very simple, used to define an access permission,

introduce the initialization file.

Initialization file

Character set

Path

Configuration file

          Automatic loading

System errors display_errors='on' error_reporting =E_ALL

Mainly used to implement character set settings, path constant configuration, configuration files, system errors, etc., analyze the modules or behaviors in the URL, and then automatically distribute the modules and behaviors (

The essence is to instantiate the object and call the object's method ). Application

Controller C

            Mainly completes business logic processing according to user requests. Call the model (M), receive data, and call the view class to process the data and echo the data to the user.

Model M

A model corresponds to a data table, and the model is reflected in the code as a class. The methods in the class are SQL statements that operate on the data table based on the user's business request (the user's request is ultimately reflected in the model as an SQL statement).

  The execution of SQL statements is performed by the DB class (the self-encapsulated PDO class is used in this framework).

View V

Mainly used to display data in HTML pages.

Picture drawn by myself:

Advantages of MVC: Each performs its own duties without interfering with each other, which is conducive to division of labor in development;

                          Conducive to code reuse;

The mainstream frameworks on the market basically meet the MVC idea.

2. Build the framework myPHP

There will be many files used in a website. A reasonable approach is to use directories to store them in categories.

1. Directory structure:

For a website that has a front-end part and a back-end part, there are two implementation solutions:

1. Dual hosts (two domain names), the front desk and the back desk each correspond to one domain name. Safe

2. The front and backend share the same host. Convenient (first plan)

X: According to the path of your own environment, myphp, the website root directory

X: According to the path of your own environment myphpAdmin website backend main directory

The folder created here:

2. Create a host

Configure in the httpd-vhosts.conf configuration file

3. Entry file

Create the index.php file under /myphp/

Description:

   Define the ACCESS constant in the entry file, and judge this constant in all subsequent PHP files. If it exists, it means legal access, if not, it means illegal access

Since the namespace is used in the imported Application.class.php file, you need to use unqualified access when accessing the Application class. CoreApplication::run();

Expand:

Another way to achieve a single entry point for a project is to use Apache’s rewrite mechanism.

4. Initialization file

a. Create the Application.class.php file in the core directory

First test whether it is accessible;

Note: I will not screenshot the class name below, all methods are within the class.

b. Create the setChar method in the Application class to complete the character set setting

After writing a private static method, you need to access the public static method as a single outlet to make it effective: Note: I will not take more screenshots of the single outlets of other methods below, they are all here.

c. Set the system error handling method

Generally when doing development, in order to prevent users from seeing error messages and unfriendly interfaces, it is generally turned off in the configuration file display_errors=off; of php.ini

d, define directory constants

Since files are frequently imported into php files, the imported files are stored in the directory. For unified management and convenient maintenance, the absolute path of the directory is set as a directory constant

(echo, var_dump is to perform some debugging to check whether the information you want is correct)

e, introduce configuration file

The configuration information in the configuration file is generally information that may change in the website, but rarely changes

Create the configuration file myphp/config/config.php

Description:

The return in the php file is to return the data to the file containing statement (include, require)

Summary:

Global variables Only in the global world

Local variables are only within the defined function

Class attributes can be used across methods

Global constants can be used across classes

$_SESSION Can be used across scripts

f, automatic loading

__autoload(); Called by the PHP autoloading mechanism

spl_autoload_register(); Provided by PHP, we can add multiple functions similar to __autoload() to the autoloading mechanism.

Debugging is to be tested on a single entry:

Automatic loading of other folders: Generally only folders with class files are loaded

Register the specified function as an auto-loading function:

Instructions

All loadCore, loadController, loadModel, etc. are called by autoLoad,

autoLoad is called again in the

run method

All class instantiation and class method movement are in index.php

5. Analyze URL

The essence of all requests is a URL, all requesting the Application. If you want the Application to accurately know the user's request and which method of which class file should be processed, then the website must have certain settings for all URLs. rules, class file names must also have certain rules:

l URL rules:

http://localhost/index.php?module=class name&action=method name in the class

l Class naming rules:

If it is a controller class:

Class name Controller File name: Class name Controller.class.php

If it is a model class:

Class name Model File name: Class name Model.class.php

6. Distribution request

(The essence is to dynamically instantiate the controller and call the controller's method)

7. Open session

Note: All methods must be accessed from a single exit

Summary:

l All php files are run in the index.php file.

l application only processes controller files

l controller controller file processes model model file

l The reason why the application can accurately call the methods in the class according to the user's request depends entirely on the URL rules and class naming rules we define.

Application namespace:

The naming rule for namespaces is to use the directory name where the file is located as the namespace name.

If space is involved, then you must consider that there will be a space name before the class name.

8. Effect display

When accessing the URL, the default route will be forwarded:

Access methods of other controllers through url

Added: pathinfo mode can be passed $_SERVER['PATH_INFO'];

That’s roughly it. I’ll add some others when I have time later. I still have a lot to learn.

Motto: Life is about constantly learning and learning again.

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