Quickly and easily build your own PHP MVC framework

Quickly and easily build your own PHP MVC framework
Introduction
MVC framework is quite popular in current development, whether you are using JAVA, C#, PHP or IOS, you will definitely choose a framework. While there is no guarantee that 100% of development languages ​​will use frameworks, the best way to understand MVC is to write your own MVC framework. In this article, Brothers www.lampbrother.net will show you how to build your own MVC framework.

MVC Architecture Pattern
M: Model-Model

V: View-View
C: Controller-Controller


The key concept of MVC is to distribute business logic from the view layer. First explain how the following HTTP requests and responses work. For example, if we have a store website and we want to add a product, the simplest URL would be something like this:

http://bestshop.com/index.php?p=admin&c=goods&a=add

http ://bestshop.com is the main domain name or basic URL;

p=admin means it is in the management module, or the background module of the system. At the same time, we must also have a foreground module, which is accessible to all users (in this case, it is p=public)

c=goods&a=add means that the URL request is the add method in the goods controller.

Front-end controller design
What is in index.php in the above example? In the PHP framework it is called the entry file. This file is usually named index.php, but you can name it otherwise. The main function of this index.php is to serve as the only entry file for HTTP requests, so no matter what resource your URL requests, it must be requested through this Index.php. You might be asking why, how is it done? The front controller in PHP is implemented using the distributed configuration .htaccess of the Apache server. In this file, we can use the rewrite module to tell the Apache server to redirect to our index.php entry file, like this:

<IfModule mod_rewrite.c>

   Options +FollowSymLinks

   RewriteEngine on

   # Send request via index.php

   RewriteCond % {REQUEST_FILENAME} !-f

   RewriteCond %{REQUEST_FILENAME} !-d

   RewriteRule ^(.*)$ index.php/$1 [L]

</IfModule>
This config file is very useful when you rewrite this config file You don't need to restart Apache. But when you modify other configuration files of Apache, you need to restart Apache to take effect, because Apache will only read these configuration files when it is started.

At the same time, index.php also initializes the framework and distributes routing requests to the corresponding controllers and methods.

Our MVC directory structure
Now let's start creating our framework directory structure. We can create a folder with the name of your project, for example: /bestshop. In this folder you need to create the following folders:

/application-stores the web application directory

/framework-stores the framework file directory

/public-stores all public static resources, such as HTML files, CSS files and jJS files.

index.php-the only entry file

and then create a next-level directory under the application folder

/config-store the configuration file of the

application/controllers-the controller class of the

application/model-the model class of the

application/view-the view file of the application

Now in the application/controllers folder, we also need to create two folders, a frontend and a backend:

Similarly, create frontend and backend folders under the view:

as you can see, in the application's controllers and The backen and frontend folders are created under the view, just like our application has foreground and background functions. But why not do the same under the model?

Well, the reason here is, normally for a web app: because generally in our application, the foreground and background can actually be regarded as two "websites", but CRUD operates on the same database, which is the question When the administrator updates the price of the goods, the front-end user can immediately see the price change, because the front-end and the back-end share a database (table). So there is no need to create two more folders in the model.

: Now let's go back to the framework folder, some framework folders are named after the framework, such as "symfony". Let us quickly create the following subdirectories in the framework:

/core-framework core file directory

/database-database directory (such as database startup classes)

/helpers-helper function directory

/libraries-class library directory

Now go to the public folder and create the following Directory:

/css-stores css files

/images-stores image files

/js-stores js files

/uploads-stores uploaded files

OK. So far this is the directory structure of our mini MVC framework!

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326657712&siteId=291194637