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!

Teach you to use composer to implement route loading

## 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

Teach you to use composer to implement route loading

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:


Teach you to use composer to implement route loading

2: Load routs in the entry file index.php. php file


Teach you to use composer to implement route loading

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:


Teach you to use composer to implement route loading

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.
  1. 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.

  2. This function accepts two parameters, method" role="presentation" style="position: relative;"> methodand params, 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.

  3. __callstatic() 做的事情也很简单,分别将目标URL(即 /fuck)、HTTP方法(即 GET)和回调代码压入 routes" role="presentation" style="position: relative;">routesmethods 和 $callbacks 三个 Macaw 类的静态成员变量(数组)中。

  4. 路由文件最后一行的 Macaw::dispatch(); 方法才是真正处理当前 URL 的地方。能直接匹配到的会直接调用回调,不能直接匹配到的将利用正则进行匹配。

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!

Related labels:
source:csdn.net
Statement of this 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template