Home >Backend Development >PHP Tutorial >Example analysis of PHP multiplayer module development principles
As the best language in the world, it occupies about 80% of the web. Small and medium-sized companies basically use the lnmp architecture. When there are more than 1 or 20 developers in a warehouse, each person may develop different modules and functions, and use code version control tools such as git to open different branches. The process is probably to first set up a complete environment locally and develop it. Deploy in the test environment. After self-test or tester testing, deploy in the pre-release environment. The pre-release is basically the same as the online environment, and then the product is accepted. After the acceptance is completed, it is released online.
Because it is developed in parallel, there must be situations where several functions are accepted or tested at the same time. At this time, whose code is deployed in the pre-release environment? If you switch to A's branch, B will not be able to accept it. Therefore, we hope that there will be a multi-person development environment where everyone's development process does not affect each other. This article mainly explains in detail the principles of PHP multi-person development and what needs to be paid attention to. I hope it can help everyone.
PHP operating principle
First of all, let’s analyze the operating principle of PHP and take a look at the language characteristics of PHP. When we initiate a request from the browser, our web server (Nginx, Apache, etc.) listens to port 80 or 443. Let's look at the simplest Nginx vhost configuration:
server { listen 80; server_name test.com; root /data/gateway/html; index index.php; location ~ \.php$ { fastcgi_pass 127.0.0.1:9001; #unix:/Users/run/php-fcgi.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
Nginx listens to port 80 and uses the corresponding vhost configuration when it matches that the domain name visited by the user is test.com. PHP-FPM starts a service in the server and listens to a port (such as 9001) or a unix socket. Nginx passes the request to PHP-FPM to parse the PHP code through fastcgi_pass configuration. The PHP parser starts parsing from index.php every time. , process it all the way, perform a series of logical processing, query the database or cache and other operations, and return an HTML or other results to Nginx, and Nginx returns it to the browser. The process is as follows:
CGI: It is a protocol for data exchange between Nginx and PHP_FPM.
FastCGI: Same as CGI, it is a communication protocol, but it has some optimizations in efficiency than CGI.
PHP-CGI: It is PHP’s interface program for the CGI protocol provided by Nginx.
PHP-FPM: It is PHP's interface program for the FastCGI protocol provided by Nginx. It also provides relatively intelligent task management.
Multi-person development environment
We can see from the PHP principle that PHP is actually just an interpreted scripting language, and each request must be parsed from index.php Once, can we name many folders on the server according to the names of different developers? In each folder, clone the code repository and switch to its own branch. Then let Nginx handle the index in each person's directory. For example, directly access http://wulv.test.com/, obtain wulv in Nginx, and set root to the wulv directory, so that you can access the code in the wulv directory. You can set Nginx like this:
set $who www; if ($http_who != "") { set $who $http_who; } root /data/gateway/$who/html;
We can let the URL carry the user's directory and intercept it in Nginx. Places to carry:
host: http://wulv.test.com
path: http://www.test.com/wulv
query: http://www.test.com?http_who=wulv
This can generally achieve the requirements, but there are still some problems. For example, some links on the page are hard-coded, and there are no If you use a relative path, you will go to www.test.com as soon as you click it, or some third-party applications such as OAuth need to verify the domain name, and you will not be able to log in if it is inconsistent with the online domain name. So other ways are needed to achieve it, such as:
http request header
cookie
We can use the Modify Headers browser plug-in to modify the http request For header information, set a parameter http_who to wulv, and then obtain it in Nginx.
Expand
If conditions permit, you can actually make a gateway server and create a configuration page. Configure the directories that need to be accessed in the configuration page. Next time you visit, the gateway It will directly help you set the http header and proxy it to the corresponding server. In this way, you don’t even need to install browser plug-ins, which is more friendly to operations and product design.
Related recommendations:
Introduction to knowledge related to PHP module development
What is PHP module development? Introduction to simple php module development
Yii framework module development analysis
The above is the detailed content of Example analysis of PHP multiplayer module development principles. For more information, please follow other related articles on the PHP Chinese website!