百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
K

klein.php

> 编程语言
开源

一个快速且灵活的路由器

2.7K stars0 点赞0 次浏览
访问官网GitHub

工具介绍

一个快速且灵活的路由器

Klein.php

klein.php 是一个快速且灵活的 PHP 5.3+ 路由器

  • 灵活的正则表达式路由(灵感来自 Sinatra)
  • 一套用于快速构建 Web 应用的样板方法
  • 几乎零开销 => 每秒 2500+ 请求

快速开始

  1. 需要 PHP 5.3.x
  2. 使用 Composer 安装 Klein(推荐)或手动安装
  3. 配置 URL 重写,使所有请求都由 index.php 处理
  4. (可选)加入一些 APC 以提升性能

Composer 安装

  1. 获取 Composer
  2. 使用 php composer.phar require klein/klein 安装 Klein
  3. 将以下内容添加到应用的主 PHP 文件中:require 'vendor/autoload.php';

示例

Hello World - 必选的 hello world 示例

php
<?php
require_once __DIR__ . '/vendor/autoload.php';

$klein = new \Klein\Klein();

$klein->respond('GET', '/hello-world', function () {
    return 'Hello World!';
});

$klein->dispatch();

示例 1 - 响应所有请求

php
$klein->respond(function () {
    return 'All the things';
});

示例 2 - 命名参数

php
$klein->respond('/[:name]', function ($request) {
    return 'Hello ' . $request->name;
});

示例 3 - 如此 RESTful

php
$klein->respond('GET', '/posts', $callback);
$klein->respond('POST', '/posts', $callback);
$klein->respond('PUT', '/posts/[i:id]', $callback);
$klein->respond('DELETE', '/posts/[i:id]', $callback);
$klein->respond('OPTIONS', null, $callback);

// To match multiple request methods:
$klein->respond(array('POST','GET'), $route, $callback);

// Or you might want to handle the requests in the same place
$klein->respond('/posts/[create|edit:action]?/[i:id]?', function ($request, $response) {
    switch ($request->action) {
        //
    }
});

示例 4 - 发送对象 / 文件

…

示例 5 - 综合示例

…

路由命名空间

php
$klein->with('/users', function () use ($klein) {

    $klein->respond('GET', '/?', function ($request, $response)

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

PHPhttpphprequest-urirouter

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言