In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "how to write PHP MVC framework". In daily operation, I believe many people have doubts about how to write PHP MVC framework. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts about "how to write PHP MVC framework". Next, please follow the editor to study!
1 what is MVC
MVC pattern (Model-View-Controller) is a kind of software architecture pattern in software engineering. MVC 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. 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 sent 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 web, and this tutorial is not intended to develop a comprehensive and ultimate MVC framework solution. We see it as a good opportunity to learn PHP from within. In the process, you will learn about object-oriented programming and MVC design patterns, as well as some considerations in development. More importantly, through the homemade MVC framework, everyone has complete control over their own framework and integrates your ideas into your framework. Isn't that a wonderful thing?
3 preparation work 3.1 Environmental preparation
Here we need the most basic PHP environment:
Nginx or Apache
PHP5.4+
MySQL
It is recommended to use phpStudy or docker to install such a LNMP environment with one click.
3.2 Code Specification
After the directory is set up, we then specify the specification of the code:
1. The table name of MySQL should be lowercase or underlined, such as item,car_orders.
2, the module name (Models) needs to use the big hump naming method, that is, the first letter is capitalized, and add Model after the name, such as: ItemModel,CarModel.
3. The controller (Controllers) needs to use the big hump naming method, that is, the first letter is capitalized, and add Controller after the name, such as: ItemController,CarController.
4. The method name (Action) needs to be named with a small hump, that is, the first letter is lowercase, such as: index,indexPost.
5. The Views deployment structure is the controller name / behavior name, such as item/view.php,car/buy.php.
The above rules are for programs to better call each other. Then it's time to start real PHP MVC programming.
3.3 Catalog preparation
Before we start development, let's give this framework a name: the Fastphp framework. Then create the project directory as needed. Suppose the project we set up is project, and the directory structure looks like this:
Project WEB deployment root directory ├─ app application directory │ ├─ controllers controller directory │ ├─ models module directory │ ├─ views view directory ├─ config configuration file directory ├─ fastphp framework core directory │ ├─ base MVC base class directory │ ├─ db database operation class directory │ ├─ Fastphp.php kernel files ├─ static static files directory ├─ index.php entry files
Then follow the next step to configure the Nginx or Apache site root to the project directory.
3.4 redirect
The purpose of redirection is twofold: to set the root directory to the location of project, and to send all requests to the index.php file. If it is an Apache server, create a new .htaccess file in the project directory with the following contents:
# turn on the Rerite function RewriteEngine On # if the request is a real file or directory, directly access RewriteCond% {REQUEST_FILENAME}!-f RewriteCond% {REQUEST_FILENAME}!-d # if the accessed file or directory does not exist, distribute the request to index.php RewriteRule. Index.php
If it is a Nginx server, modify the configuration file and add the following redirection to the server block:
Location / {# re-request all non-existent requests to index.php try_files $uri $uri/ / index.php$args;}
The main reasons for this are:
(1) static files can be accessed directly. If the file or directory exists, access the existing file / directory directly. For example, if the static file static/css/main.css is real, you can access it directly. (2) there is a single entrance to the program. In this case, the request address is not a real file or directory, so the request will be sent to the index.php. For example, the access address: localhost/item/detail/1, there is no such file or directory in the file system. Then the Apache or Nginx server sends the request to index.php and assigns the string after the domain name to the REQUEST_URI variable. In this way, you can get / item/detail/1; with $_ SERVER ['REQUEST_URI'] in PHP. / item/detail/1; (3) can be used to generate beautified URL for SEO.
4 PHP MVC core files 4.1 entry files
Next, create a new index.php entry file under the project directory with the following contents:
.
The main reasons for this are:
For files with only PHP code, it's best not to have a closing flag? >
PHP itself does not need a closing symbol, and not adding a Terminator makes the program more secure, largely preventing the end from being injected with additional content.
4.2 profile
In the entry file, we loaded the contents of the config.php file, so what does it do? As you can easily see from the name, its purpose is to save some common configurations. The contents of the config.php file are as follows to define the database connection parameter parameters and to configure the default controller name and operation name:
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.