Table of Contents
用 Composer构建自己的 PHP 框架之构建路由,composer构建
我想搭建自己的php开发环境,业界常见都是什框架+什模板引擎?
php怎学会一个框架的路由机制?
Home php教程 php手册 用 Composer构建自己的 PHP 框架之构建路由,composer构建

用 Composer构建自己的 PHP 框架之构建路由,composer构建

Jun 13, 2016 am 09:22 AM
composer php frame routing

用 Composer构建自己的 PHP 框架之构建路由,composer构建

上一篇中我们已经建立了一个空的 Composer 项目,本篇将讲述如何构建路由。

久负盛名的 CodeIgniter 框架是很多人的 PHP 开发入门框架,同样也是我开始学习如何从头构建一个网站的框架。在 CI 中我学到了很多,其中对 MVC 的深入理解和对框架本质的理解对我的影响最大。从使用框架是为了提高开发效率的角度来看,框架的本质就是路由。

下面我们就开始自己来构建路由,先去 GitHub 搜一下:点此查看搜索结果

推荐https://github.com/NoahBuscher/Macaw,对应的 Composer 包为codingbean/macaw ,作者应该是在 GitHub 上改名了,这可能会造成一定的困扰。下面开始安装 Macaw 包,更改 composer.json:

{ "require": {  "codingbean/macaw": "dev-master" }}
Copy after login

运行 composer update,成功之后将得到以下目录:


至此,Macaw 包安装成功!

下面,就是见证奇迹的时刻!我们将赋予 MFFC 生命力,让它真正地跑起来!

新建 MFFC/public 文件夹,这个文件夹将是用户唯一可见的部分。在文件夹下新建 index.php 文件:

Copy after login

上面一行表示引入 Composer 的自动载入功能,下面一行表示载入路由配置文件。新建 MFFC/config 文件夹,在里面新建 routs.php 文件,内容如下:

'.$fu;});Macaw::dispatch();<br>
Copy after login

Macaw 的文档位于https://github.com/NoahBuscher/Macaw,请按照你的 HTTP 服务软件类型自行设置伪静态,其实跟绝大多数框架一样:“将所有非静态文件全部指向 index.php”。

然后,将某一个端口用 Apache 或 Nginx 分配给 MFFC/public 目录,这一步十分建议用 Apache 或者 Nginx 做。

如果使用 PHP 内置 HTTP 服务器:

cd public && php -S 127.0.0.1:3000
Copy after login

将导致路由的Macaw::get('fuck' 必须写成Macaw::get('/fuck' 才能响应。

目前的代码使用 Apache + mod_php 和 Nginx + php-fpm 方式均没有问题。

我在本地绑定了 81 端口,访问http://127.0.0.1:81/fuck 可以看到:


如果页面乱码,请调整编码为 UTF-8。如果你成功看到以上页面,那么恭喜你,路由配置成功!

Macaw 只有一个文件,去除空行总共也就一百行多一点,通过代码我们能直接看明白它是怎么工作的。下面我简略分析一下:

1. Composer 的自动加载在每次 URL 驱动 MFFC/public/index.php 之后会在内存中维护一个全量命名空间类名到文件名的数组,这样当我们在代码中使用某个类的时候,将自动载入该类所在的文件。

2. 我们在路由文件中载入了 Macaw 类:“use NoahBuscher\Macaw\Macaw;”,接着调用了两次静态方法 ::get(),这个方法是不存在的,将由MFFC/vendor/codingbean/macaw/Macaw.php 中的__callstatic() 接管。

3. 这个函数接受两个参数,$method 和 $params,前者是具体的 function 名称,在这里就是 get,后者是这次调用传递的参数,即 Macaw::get('fuck',function(){...}) 中的两个参数。第一个参数是我们想要监听的 URL 值,第二个参数是一个 PHP 闭包,作为回调,代表 URL 匹配成功后我们想要做的事情。

4.__callstatic() 做的事情也很简单,分别将目标URL(即 /fuck)、HTTP方法(即 GET)和回调代码压入$routes、$methods 和$callbacks 三个 Macaw 类的静态成员变量(数组)中。

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

我想搭建自己的php开发环境,业界常见都是什框架+什模板引擎?

mysql+php+apache,模板用smarty好了,简单又好用。
 

php怎学会一个框架的路由机制?

如果用过zend和symphony或者yii的可以去研究一下源码。
主要是通过伪静态来实现单一入口,mvc框架都是这样。将所有请求通过apache伪静态解析转给某个指定文件,然后通过php的$_SERVER[]这个全局变量来去的请求的路径字符串,对其进行解析再分配给指定的类去处理,基本就是这样。
最简单的方法是建立一个.htaccess文件。文件内容贴给你

ErrorDocument 500 'mod_rewrite must be enabled'

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php

将这个文件放在根目录下即可。文件作用很明显:将所有的请求路径转给index.php,然后在index.php里做请求字符串的判断即可打到路由解析功能。
 

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

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1662
14
PHP Tutorial
1262
29
C# Tutorial
1235
24
Use Composer to solve the dilemma of recommendation systems: andres-montanez/recommendations-bundle Use Composer to solve the dilemma of recommendation systems: andres-montanez/recommendations-bundle Apr 18, 2025 am 11:48 AM

When developing an e-commerce website, I encountered a difficult problem: how to provide users with personalized product recommendations. Initially, I tried some simple recommendation algorithms, but the results were not ideal, and user satisfaction was also affected. In order to improve the accuracy and efficiency of the recommendation system, I decided to adopt a more professional solution. Finally, I installed andres-montanez/recommendations-bundle through Composer, which not only solved my problem, but also greatly improved the performance of the recommendation system. You can learn composer through the following address:

Laravel Introduction Example Laravel Introduction Example Apr 18, 2025 pm 12:45 PM

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

The Continued Use of PHP: Reasons for Its Endurance The Continued Use of PHP: Reasons for Its Endurance Apr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

Laravel framework installation method Laravel framework installation method Apr 18, 2025 pm 12:54 PM

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

How to view the version number of laravel? How to view the version number of laravel How to view the version number of laravel? How to view the version number of laravel Apr 18, 2025 pm 01:00 PM

The Laravel framework has built-in methods to easily view its version number to meet the different needs of developers. This article will explore these methods, including using the Composer command line tool, accessing .env files, or obtaining version information through PHP code. These methods are essential for maintaining and managing versioning of Laravel applications.

Using Dicr/Yii2-Google to integrate Google API in YII2 Using Dicr/Yii2-Google to integrate Google API in YII2 Apr 18, 2025 am 11:54 AM

VprocesserazrabotkiveB-enclosed, Мнепришлостольностьсясзадачейтерациигооглапидляпапакробоглесхетсigootrive. LEAVALLYSUMBALLANCEFRIABLANCEFAUMDOPTOMATIFICATION, ČtookazaLovnetakProsto, Kakaožidal.Posenesko

How to simplify email marketing with Composer: DUWA.io's application practices How to simplify email marketing with Composer: DUWA.io's application practices Apr 18, 2025 am 11:27 AM

I'm having a tricky problem when doing a mail marketing campaign: how to efficiently create and send mail in HTML format. The traditional approach is to write code manually and send emails using an SMTP server, but this is not only time consuming, but also error-prone. After trying multiple solutions, I discovered DUWA.io, a simple and easy-to-use RESTAPI that helps me create and send HTML mail quickly. To further simplify the development process, I decided to use Composer to install and manage DUWA.io's PHP library - captaindoe/duwa.

laravel installation code laravel installation code Apr 18, 2025 pm 12:30 PM

To install Laravel, follow these steps in sequence: Install Composer (for macOS/Linux and Windows) Install Laravel Installer Create a new project Start Service Access Application (URL: http://127.0.0.1:8000) Set up the database connection (if required)

See all articles