In the previous article we have established an empty Composer project, which will describe how to build a route.
The prestigious CodeIgniter framework is an introductory framework for many people in PHP development, as well as a framework for me to learn how to build a Web site from scratch. I have learned a lot in CI, in which the deep understanding of MVC and understanding of the nature of the framework has the greatest impact on me. From the perspective of using frameworks to improve development efficiency, the essence of a framework is routing.
Below we start to build the route ourselves, first go to GitHub search: Click here to view the results
Recommended Https://github.com/NoahBuscher/Macaw, the corresponding Composer package for Codingbean/macaw, the author should be renamed on GitHub, which may cause some trouble. The following starts with installing the Macaw package and changing the Composer.json:
{"Require": {"Codingbean/macaw": "Dev-master"}}
After you run composer update, you will get the following directory:
At this point, Macaw package successfully installed!
Below, is the time to witness miracles! We will give MFFC vitality, let it really run!
Create a new Mffc/public folder, which will be the only visible part of the user. Create a new index.php file under a folder:
<?php//AutoLoad automatically loaded into require '. /vendor/autoload.php '//Routing configuration require ' ... /config/routes.php ';
The line above represents the automatic loading feature that introduces Composer, and the following line represents the load route configuration file. Create a new Mffc/config folder and create a new routs.php file in it, which reads as follows:
<?phpuse Noahbuscher\macaw\macaw; Macaw::get (' Fuck ', function () {echo success! ";}); Macaw::get (' (: All) ', function ($FU) {echo ' does not match to Route <br> '. $fu;}); Macaw::d ispatch ();
Macaw's documentation is located in Https://github.com/NoahBuscher/Macaw, please set the pseudo static for your HTTP service software type, in fact, like most frameworks: "Point all non-static files to index.php."
Then, assign a port to the Mffc/public directory with Apache or Nginx, a step that is highly recommended for Apache or Nginx.
If you are using a PHP built-in HTTP server:
CD Public && php-s 127.0.0.1:3000
The Macaw::get (' fuck ') that will cause the route must be written as a macaw::get ('/fuck ') to respond.
The current code uses Apache + mod_php and Nginx + PHP-FPM methods without problems.
I have a local 81 port bound, Access Http://127.0.0.1:81/fuck can see:
If the page is garbled, please adjust the encoding to UTF-8. If you successfully see the above page, then congratulations, routing configuration success!
Macaw only one file, the total number of stripped empty lines is a little more than 100 lines, through the code we can see directly how it works. Below I briefly analyze:
1. Composer automatic loading maintains an array of full namespace class names to file names in memory after each URL-driven mffc/public/index.php, so that when a class is used in code, the file in which the class resides is automatically loaded.
2. We loaded the Macaw class in the routing file: "Use Noahbuscher\macaw\macaw;" and then called Two static methods:: Get (), this method does not exist, will be by mffc/vendor/codingbean/macaw/ Macaw.php in the __callstatic () take over.
3. This function accepts two parameters, $method and $params, which is the specific function name, where get, which is the parameter passed by this call, that is, the two parameters in Macaw::get (' Fuck ', function () {...}). The first parameter is the URL value we want to listen to, and the second parameter is a PHP closure, as a callback that represents what we want to do after the URL match succeeds.
4.__callstatic () Does the simple thing of pressing the target URL (that is,/fuck), the HTTP method (that is, get) and the callback code into the static member variables (arrays) of the $routes, $methods, and $callbacks three macaw classes.
5. Macaw of the last line of the routing file::d ispatch (); Method is where the current URL is actually handled. Direct matching to the callback will be directly invoked, not directly matched to the use of a regular match.