Build a route using Composer to build your own PHP framework and composer to build a route
In the previous article, we have created an empty Composer project. This article describes how to build a route.
The prestigious CodeIgniter framework is an entry-level framework for PHP development by many people. It is also a framework for me to learn how to build a website from scratch. I have learned a lot in CI, and the in-depth understanding of MVC and the understanding of the framework nature have the greatest impact on me. From the perspective that the framework is used to improve development efficiency, the essence of the Framework is routing.
Next, we will start to build the route by ourselves. Go to GitHub and search for the route: click here to view the search results.
We recommend https://github.com/noahbuscher/macaw. the corresponding Composer package is codingbean/macaw. The author should have changed its name on GitHub, which may cause some troubles. Install the Macaw package and change composer. json:
{ "require": { "codingbean/macaw": "dev-master" }}
Run composer update. After the update is successful, the following directory is displayed:
So far, Macaw package is successfully installed!
Here is the time to witness the miracle! We will give MFFC vitality and let it really run!
Create a new MFFC/public folder, which is the only visible part of the user. Create an index. php file in the folder:
<? Php // Autoload automatically loads require '../vendor/autoload. php'; // route configuration require' ../config/routes. php ';
The above line indicates the automatic loading function of Composer is introduced, and the following line indicates loading the route configuration file. Create the MFFC/config folder and create the routs. php file in it. The content is as follows:
<? Phpuse NoahBuscher \ Macaw; Macaw: get ('fuck', function () {echo "successful! ";}); Macaw: get ('(: all)', function ($ fu) {echo 'does not match the route <br> '. $ fu;}); Macaw: dispatch ();
The Macaw documentation is located at https://github.com/noahbuscher/macaw. follow the instructions in the HTTP service software type to set pseudo-static. In fact, like the vast majority of frameworks: "All non-static files are directed to index. php ".
Then, use Apache or Nginx to allocate a port to the MFFC/public directory. Apache or Nginx is recommended for this step.
If you use the PHP built-in HTTP Server:
cd public && php -S 127.0.0.1:3000
The Macaw: get ('fuck' must be written as Macaw: get ('/fuck') that causes the route to respond.
The current Code uses Apache + mod_php and Nginx + php-fpm methods.
I locally bound port 81 and accessed http: // 127.0.0.1: 81/fuck. You can see:
If the page is garbled, adjust the encoding to UTF-8. If you see the preceding page successfully, congratulations, route configuration is successful!
Macaw has only one file, and a total of more than one hundred lines are removed from blank lines. Through code, we can directly see how it works. The following is a brief analysis:
1. the Composer is automatically loaded at each URL to drive MFFC/public/index. php will then maintain an array of full namespace class names to file names in the memory, so that when we use a class in the code, it will automatically load the file where the class is located.
2. we loaded the Macaw class in the Routing File: "use NoahBuscher \ Macaw;" and then called the static method twice: get (). This method does not exist, by MFFC/vendor/codingbean/macaw/Macaw. in php, _ callstatic () takes over.
3. this function accepts two parameters: $ method and $ params. The former is the specific function name, where get is used. The latter is the parameter passed in this call, that is, 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, it indicates what we want to do after successful URL matching.
4. _ callstatic () is also very easy to do, respectively set the target URL (I .e./fuck), HTTP method (I .e. GET) and the callback code is pushed into the static member variables (arrays) of the $ routes, $ methods, and $ callbacks Macaw classes.
5. the Macaw: dispatch (); method of the last line of the route file is the place where the current URL is actually processed. Callback is called directly if the matching result can be directly matched. If the matching result cannot be directly matched, the regular expression is used for matching.
I want to build my own php development environment. What is the common framework + template engine in the industry?
Mysql + php + apache, the template is smarty, simple and easy to use.
How does php learn a framework routing mechanism?
If you have used zend, symphony, or yii, you can study the source code.
It mainly implements a single entry through pseudo-static. The mvc framework is like this. Transfers all requests to a specified file through apache pseudo-static parsing, and then uses the php $ _ SERVER [] global variable to return the request path string, this is basically the case when parsing and allocating it to the specified class.
The simplest method is to create a. htaccess file. Paste the file content to you
<IfModule! Mod_rewrite.c>
ErrorDocument 500 'mod _ rewrite must be enabled'
</IfModule>
RewriteEngine on
RewriteCond % {REQUEST_FILENAME }! -F
RewriteCond % {REQUEST_FILENAME }! -D
RewriteRule ^ (. *) $ index. php
Put this file in the root directory. File function is obvious: transfer all request paths to index. php, and then make the request string judgment in index. php to hit the routing parsing function.