Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write a PHP framework to gain an in-depth understanding of the MVC running process

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article shows you how to write a PHP framework to gain an in-depth understanding of the MVC running process, which is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

1 what is MVC

MVC pattern (Model-View-Controller) is a software architecture pattern in software engineering, which divides the software system into three basic parts: Model, View and Controller.

The MVC model in PHP, also known as Web MVC, evolved from the 1970s. The purpose of MVC is to realize a dynamic program design, which is convenient for subsequent modification and expansion of the program, and makes it possible to reuse a certain part of the program. In addition, this mode makes the program structure more intuitive by simplifying the complexity. The software system not only separates the basic parts of itself, but also gives each basic part its due function.

Functions of each part of MVC:

Model Model-manages most of the business logic and all the database logic. The model provides an abstraction layer for connecting and manipulating databases.

Controller Controller-responsible for responding to user requests, preparing data, and deciding how to display the data.

View View-responsible for rendering the data and presenting it to the user through HTML.

A typical Web MVC process:

1.Controller intercepts requests made by users

2.Controller calls the read and write operation of Model completion status

3.Controller passes the data to View

4.View renders the final result and presents it to the user.

2 Why should you develop your own MVC framework

There are a large number of excellent MVC frameworks available on the network, this tutorial is not to develop a comprehensive, ultimate MVC framework solution, but as a good opportunity to learn PHP from within, in the process, you will learn object-oriented programming and MVC design patterns, and learn some notes in development.

More importantly, you can have complete control over your framework and incorporate your ideas into the framework you develop. Although it may not be done well, you can develop functions and modules in your own way.

3 start to develop your own MVC framework

3.1 Catalog preparation

Before starting development, let's set up the project. Assuming that the framework we built for todo,MVC can be named FastPHP, then the first step is to set up the directory structure.

Although all of the above directories will not be used in this tutorial, it is necessary to set up the program directory at the beginning for the sake of future program extensibility. Let's talk about the role of each directory:

Application-Application Cod

Config-Program configuration or database configuration

Fastphp-Framework core directory

Public-static Fil

Runtime-temporary data directory

Scripts-Command Line tool

3.2 Code Specification

After the directory is set up, we are going to specify the specification of the code:

The table name of 1.MySQL should be lowercase, such as item,car

two。 The module name (Models) should be capitalized and followed by "Model", such as ItemModel,CarModel.

3. The controller (Controllers) needs to capitalize and add "Controller" to the name, such as: ItemController,CarController

4. View (Views) deployment structure is "controller name / behavior name", such as item/view.php,car/buy.php

Some of the above rules are designed to better call each other in the program. Then it's time to start real PHP MVC programming.

3.3 Redirect

Redirect all data requests to the index.php file, and create a new .htaccess file in the todo directory with the following contents:

RewriteEngine On # ensure that the request path is not a file name or directory RewriteCond% {REQUEST_FILENAME}!-f RewriteCond% {REQUEST_FILENAME}!-d # redirect all requests to index.php?url=PATHNAME RewriteRule ^ (. *) $index.php?url=$1 [PT,L]

The main reasons for this are:

1. The program has a single entry

two。 All programs except static programs are redirected to index.php

3. It can be used to generate URL that is good for SEO. If you want to configure URL better, you may need URL routing later. Let's not introduce it here.

3.4 entry file

After doing the above, we should know what we need to do, yes! Add the index.php file in the public directory with the following contents:

.item {width:400px;} input {color:#222222; font-family:georgia,times; font-size:24px; font-weight:normal; line-height:1.2em; color:black;} a {color:blue; font-family:georgia,times; font-size:20px; font-weight:normal; line-height:1.2em Text-decoration:none;} a:hover {text-decoration:underline;} h2 {color:#000000; font-size:41px; letter-spacing:-2px; line-height:1em; font-family:helvetica,arial,sans-serif; border-bottom:1px dotted # cccccc;} h3 {color:#000000; font-size:34px; letter-spacing:-2px Line-height:1em; font-family:helvetica,arial,sans-serif;} footer.php, content:

Then, create the following view files in views/item.

Index.php, browse all the records of the item table in the database, contents:

/ item/view/ "title=" Click modify >

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report