Teach you to use composer to implement route loading
藏色散人
Release: 2020-07-10 14:10:06
forward
3908 people have browsed it
The following tutorial column of composer will introduce to you how to use composer to implement route loading. I hope it will be helpful to friends in need!
## See the sample code for this tutorial at https:/ /github.com/johnlui/My-First-Framework-based-on-Composer
The prestigious CodeIgniter framework is the introductory framework for PHP development for many people. It is also the starting point for me to learn how to build a website from scratch. s frame. 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.
Recommend https://github.com/NoahBuscher/Macaw, the corresponding Composer package is noahbuscher/macaw.
Let’s start installing it and change composer.json:
"require": {
"noahbuscher/macaw": "dev-master"
},
Copy after login
Run composer install and get the following directory after success
At this point, the Macaw package is installed successfully! The following is the moment to witness the miracle! We will give MFFC life and make it really run! 1: Create a new App/config folder and create a new routs.php file in it with the following content:
2: Load routs in the entry file index.php. php file
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". Access the domain name after successful configuration:
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:
Composer's automatic loading will maintain an array in memory from the full namespace class name to the file name after each URL drives MFFC/public/index.php, so When we use a class in the code, the file where the class is located will be automatically loaded.
We loaded the Macaw class in the routing file: "use NoahBuscher\Macaw\Macaw;", and then called the static method::get() twice. This method does not exist. Will be taken over by __callstatic() in MFFC/vendor/codingbean/macaw/Macaw.php.
This function accepts two parameters, method和" role="presentation" style="position: relative;">m ethod##andparams, the former is the specific function name, here it is get, the latter is the parameter passed in this call, that is, Macaw::get('fuck',function(){ …}) in the two parameters. 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.
The above is the detailed content of Teach you to use composer to implement route loading. For more information, please follow other related articles on the PHP Chinese website!
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn