Write your own simple MVC Framework myPHP and mvc Framework myphp
MyPHP framework
It adopts the MVC idea and applies the object-oriented method and a single project portal to implement a custom framework. (Exercises you are interested in)
I. single project portal File
Myphp \ index. php foreground
All requests from a website request an object (entry file) index. php \ admin. php. The entry is very simple. It is used to define an access permission and introduce the initialization file.
Initialize File
Character Set
Path
Configuration File
Automatic Loading
System Error display_errors = 'on' error_reporting = E_ALL
It is mainly used to set character sets, configure path constants, configuration files, system errors, etc. It analyzes modules or actions in URLs, and then automatically distributes modules and actions (essentially instantiates objects, and call the object method ). Application
Controller C
The Service Logic is processed based on user requests. Call model (M) to receive data, call the View class to process data, and echo the data to the user.
Model M
A model corresponds to a data table, and the model is a class in the code. The method in the class is the SQL statement used to operate 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 PDO class is encapsulated in this framework.
View V
It is mainly used to display data in HTML pages.
Self-drawn graph:
Advantages of MVC: performing their respective duties without interfering with each other is conducive to the Division of Labor in Development;
Conducive to code reuse;
The mainstream frameworks in the market basically satisfy the idea of MVC.
Ii. Build a framework myPHP
Many files are used in a website. A proper method is to store directories in different categories.
1. directory structure:
There are two Implementation Solutions for a website that includes the front-end and back-end:
1. Dual-host (two domain names), one domain name for each front-end and backend. Security
2. the front and back ends share one host. Convenience (solution first)
X: \ Based On Your Own Environment path \ myphp website root directory
X: \ Based On Your Own Environment path \ myphp \ Admin website background main directory
The folder created here:
2. Create a host
Configuration in the httpd-vhosts.conf Profile
3. Entry File
Create the index. php file under/myphp/
Note:
Define an ACCESS constant in the entry file and judge this constant in all the subsequent PHP files. If yes, it indicates legal ACCESS. If no, it indicates illegal ACCESS.
Because the introduced Application. class. php file uses namespaces, you must use unrestricted access to the Application class. Core \ Application: run ();
Expansion:
Another solution for implementing a single project portal is to use Apache's rewrite mechanism.
4. initialize the file
A. Create the Application. class. php file in the core directory.
First, test whether access is allowed. Note: I will not go to the class name below, and all the methods are in the class.
B. Create the setChar method in the Application class to complete character set settings.
After writing a private static method, you need to access the public static method that acts as a single exit to make it take effect: Note: Below I will not add a single exit of other methods here.
C. Set the system error handling method
Generally, during development, the configuration file display_errors = off in php. ini is usually disabled to Prevent Users From seeing error messages and unfriendly interfaces.
D. Define directory Constants
Because PHP files frequently introduce files, the imported files are stored in the directory. for unified management and convenient maintenance, set the absolute path of the directory to a directory constant.
(Echo, var_dump is used to adjust and check whether the information you want is correct)
E. Introduce the configuration file.
The configuration information in the configuration file is generally the information that may be changed but rarely changed on the website.
Create the configuration file myphp/config. php
Note:
The return Statement in the PHP file contains the include and require statements in the returned data file)
Summary:
The global variable is only in the global
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 automatic loading mechanism
Spl_autoload_register (); multiple functions similar to _ autoload () can be added to the automatic loading mechanism provided by PHP.
Debugging should be performed at the single portal:
Automatic loading of other folders: Generally, only folders with class files are loaded.
Register a specified function as an automatic loading function:
Description
All loadCore, loadController, and loadModel are called by autoLoad,
AutoLoad is also called in the run Method
The instantiation of all classes and the movement of class methods are in index. php.
5. Analyze the URL
All requests are based on a URL and are requested to submit an Application. If you want the Application to be able to accurately know the method of the user's request, therefore, you must set rules for the website to occupy all URLs and for the class file names:
L URL rules:
Http: // localhost/index. php? Module = Class Name & action = method name in the class
L class naming rules:
For the Controller class:
Class Name Controller file name: class Name Controller. class. php
For model classes:
Class Name Model file name: class Name Model. class. php
6. Distribute requests
(The essence is to dynamically instantiate the controller and call the Controller method)
7. Start a session
Note: All methods must be accessed at a single exit.
Summary:
L all PHP files are running in the index. php file.
L application only processes controller files
L controller File Processing model File
L The reason why the application can accurately call methods in the class based on user requests is completely dependent on the url rules we define and the class naming rules.
Application namespace:
The naming rule for a namespace is the namespace name in the directory where the file is located.
If space is involved, you must consider that there is a space name before the class name.
8. Display Results
When you access the URL, the default route is forwarded:
Access other controllers through url
Supplement: In the pathinfo mode, you can use $ _ SERVER ['path _ info'];
This is basically the case. I will try other things later, and I still need to learn more.
Motto: Life is constantly learning and learning.