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)
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 filePath
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
Model M
The execution of SQL statements is performed by the DB class (the self-encapsulated PDO class is used in this framework).
View V
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
1. Directory structure:
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
3. Entry file
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 functionClass 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 mechanismspl_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 URLl 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
(The essence is to dynamically instantiate the controller and call the controller's method)
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.
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.