Home >Backend Development >PHP Tutorial >Use Composer to build your own PHP framework to build routes, composer build_PHP tutorial
In the previous article, we have created an empty Composer project. This article will describe how to build routing.
The well-known CodeIgniter framework is the entry-level framework for PHP development for many people. It is also the framework where I started learning how to build a website from scratch. I learned a lot in CI, among which the in-depth understanding of MVC and the understanding of the nature of the framework had the greatest impact on me. From the perspective of using frameworks to improve development efficiency, the essence of frameworks is routing.
Let’s start building the route ourselves. First, search on GitHub: Click here to view the search results
Recommend https://github.com/NoahBuscher/Macaw. The corresponding Composer package is codingbean/macaw. The author should have changed the name on GitHub, which may cause some trouble. Let’s start installing the Macaw package and change composer.json:
{ "require": { "codingbean/macaw": "dev-master" }}
Run composer update. After success, you will get the following directory:
At this point, the Macaw package is successfully installed!
The following is the moment to witness the miracle! We will give MFFC life and make it really run!
Create a new MFFC/public folder, this folder will be the only part visible to users. Create a new index.php file in the folder:
<?php// Autoload 自动载入require '../vendor/autoload.php';// 路由配置require '../config/routes.php';<br />
The upper line indicates the introduction of Composer’s automatic loading function, and the lower line indicates the loading of the routing configuration file. Create a new MFFC/config folder and create a new routs.php file in it with the following content:
<?phpuse NoahBuscher\Macaw\Macaw;Macaw::get('fuck', function() { echo "成功!";});Macaw::get('(:all)', function($fu) { echo '未匹配到路由<br>'.$fu;});Macaw::dispatch();<br>
Macaw's documentation is located at https://github.com/NoahBuscher/Macaw. Please set up pseudo-static according to your HTTP service software type. In fact, it is the same as most frameworks: "Point all non-static files to index. php".
Then, use Apache or Nginx to assign a certain port to the MFFC/public directory. It is highly recommended to use Apache or Nginx for this step.
If using PHP’s built-in HTTP server:
cd public && php -S 127.0.0.1:3000
The Macaw::get('fuck' that will cause routing must be written as Macaw::get('/fuck' to respond.
The current code uses Apache + mod_php and Nginx + php-fpm without problems.
I have bound port 81 locally. Visit http://127.0.0.1:81/fuck and you can see:
If the page is garbled, please adjust the encoding to UTF-8. If you successfully see the above page, congratulations, the routing configuration is successful!
Macaw has only one file, and it only takes a little more than a hundred lines to remove blank lines. We can directly see how it works through the code. Let me briefly analyze it below:
1. The automatic loading of Composer will maintain an array of full namespace class names to file names in the memory after each URL drives MFFC/public/index.php, so that when we use a certain class in the code time, the file containing the class will be automatically loaded.
2. We loaded the Macaw class in the routing file: "use NoahBuscherMacawMacaw;" and then called the static method::get() twice. This method does not exist and will be used by MFFC/vendor/codingbean/macaw __callstatic() in /Macaw.php takes over.
3. This function accepts two parameters, $method and $params. The former is the specific function name, here it is get, and the latter is the parameter passed in this call, that is, Macaw::get('fuck', two parameters in function(){...}). The first parameter is the URL value we want to monitor, and the second parameter is a PHP closure, which serves as a callback and represents what we want to do after the URL is successfully matched.
4.__callstatic() does a very simple job. It pushes the target URL (i.e. /fuck), HTTP method (i.e. GET) and callback code into the static files of the three Macaw classes $routes, $methods and $callbacks. member variable (array).
5. The Macaw::dispatch(); method in the last line of the routing file is where the current URL is actually processed. If it can be directly matched, the callback will be called directly, and if it cannot be directly matched, regular rules will be used for matching.
mysql+php+apache, use smarty for the template, it is simple and easy to use.
If you have used zend, symphony or yii, you can study the source code.
Mainly through pseudo-static to achieve a single entry, the mvc framework is like this. Transfer all requests to a specified file through apache pseudo-static parsing, and then use the $_SERVER[] global variable of PHP to obtain the path string of the request, parse it, and then assign it to the specified class for processing. Basically, so.
The easiest way is to create a .htaccess file. The file content is posted to you
ErrorDocument 500 'mod_rewrite must be enabled'
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !- f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php
Place this file in the root directory. The function of the file is obvious: transfer all request paths to index.php, and then judge the request string in index.php to access the route parsing function.