search
HomeDevelopment ToolscomposerTeach you to use composer to implement route loading

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"
  },
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!

Statement
This article is reproduced at:csdn. If there is any infringement, please contact admin@php.cn delete
Composer: Streamlining PHP Project DevelopmentComposer: Streamlining PHP Project DevelopmentApr 15, 2025 am 12:08 AM

Composer simplifies PHP project development steps include: 1) adding dependencies, such as adding "laravel/framework":"^8.0" in composer.json; 2) automatically loading, generating an autoload.php file by defining the namespace and classpath; 3) optimizing performance, using composerdump-autoload-o and composerinstall--no-dev-optimize-autoloader commands. Through these steps, developers can be more efficient and avoid common mistakes.

Composer Credentials: What Makes Someone a Composer?Composer Credentials: What Makes Someone a Composer?Apr 14, 2025 am 12:13 AM

The key elements of becoming a composer include: 1. Mastering music theory, 2. Being creative and original, 3. Having technical skills, 4. Being able to express emotions and tell stories, 5. Understand cultural and historical background, 6. Accumulating practice and experience, these elements together constitute the composer's identity and ability.

The Path to Becoming a Composer: A Practical GuideThe Path to Becoming a Composer: A Practical GuideApr 13, 2025 am 12:11 AM

The steps to becoming a composer include: 1. Master the basic elements of music, such as notes, rhythm, harmony, and melody; 2. Select appropriate technical tools, such as AbletonLive; 3. Understand the process of composing, including inspiration acquisition, conception, writing, modification and improvement; 4. Start with simple melody creation and gradually try complex techniques such as harmony; 5. Solve common problems through debugging techniques, such as note selection and rhythm arrangement; 6. Apply performance optimization and best practices, such as using templates, version control, and collaboration.

Composer: The Key to Building Robust PHP ApplicationsComposer: The Key to Building Robust PHP ApplicationsApr 12, 2025 am 12:05 AM

Composer is a key tool for building robust PHP applications because it simplifies dependency management, improves development efficiency and code quality. 1) Composer defines project dependencies through composer.json file and automatically downloads and manages these dependencies. 2) It generates a composer.lock file to ensure that the dependency version is consistent and automatically loaded through vendor/autoload.php. 3) Examples of usage include basic usage such as adding log libraries, as well as advanced usage such as version constraints and environment variable management. 4) Common error debugging techniques include handling dependency conflicts and network problems. 5) Performance optimization suggestions include using composer.lock file and optimizing automatic loading.

Composer Expertise: What Makes Someone SkilledComposer Expertise: What Makes Someone SkilledApr 11, 2025 pm 12:41 PM

To become proficient when using Composer, you need to master the following skills: 1. Proficient in using composer.json and composer.lock files, 2. Understand how Composer works, 3. Master Composer's command line tools, 4. Understand basic and advanced usage, 5. Familiar with common errors and debugging techniques, 6. Optimize usage and follow best practices.

What is a composer doing?What is a composer doing?Apr 08, 2025 am 12:19 AM

Composer is a dependency management tool for PHP, used to declare, download and manage project dependencies. 1) Declare dependencies through composer.json file, 2) Install dependencies using composerinstall command, 3) parse the dependency tree and download it from Packagist, 4) generate the autoload.php file to simplify automatic loading, 5) optimize use includes using composerupdate--prefer-dist and adjusting the autoload configuration.

What is App composer?What is App composer?Apr 07, 2025 am 12:07 AM

AppComposer is a tool for building and managing applications. 1) It simplifies application development and improves efficiency by dragging and configuring predefined components. 2) Developers can define components, combine interfaces, define business logic, and ultimately render the application. 3) Support basic and advanced usage, such as task management and conditional rendering, helping to build flexible applications.

What is a composer used for?What is a composer used for?Apr 06, 2025 am 12:02 AM

Composer is a dependency management tool for PHP. The core steps of using Composer include: 1) Declare dependencies in composer.json, such as "stripe/stripe-php":"^7.0"; 2) Run composerinstall to download and configure dependencies; 3) Manage versions and autoloads through composer.lock and autoload.php. Composer simplifies dependency management and improves project efficiency and maintainability.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor