lizzyman04/file-router 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

lizzyman04/file-router

Composer 安装命令:

composer require lizzyman04/file-router

包简介

Next.js-style file-based routing for PHP. Zero dependencies, framework-agnostic.

README 文档

README

Next.js-style file-based routing for PHP. The directory structure is the route table — no separate route file to keep in sync. Zero runtime dependencies, framework-agnostic, PHP 8.1+.

Extracted from Fluxor so it can be used standalone or dropped into any stack.

Install

composer require lizzyman04/file-router

Routing conventions

File on disk URL pattern Params
index.php /
about.php /about
users/[id].php /users/{id} id
posts/[id]/comments.php /posts/{id}/comments id
files/[...slug].php /files/{slug:*} (catch-all) slug (rest of path)
(admin)/dashboard.php /dashboard group folder stripped

Matching is specificity-ordered: static > dynamic > catch-all, so /users/me beats /users/[id].

Standalone usage

use FileRouter\FileRouter;

$router = new FileRouter(__DIR__ . '/routes');

$match = $router->match($_SERVER['REQUEST_METHOD'], $_SERVER['REQUEST_URI']);

if ($match === null) {
    http_response_code(404);
} elseif ($match->isMethodNotAllowed()) {
    http_response_code(405);
    header('Allow: ' . implode(', ', $match->allowedMethods));
} else {
    // $match->file   — the route file to include/dispatch
    // $match->params — e.g. ['id' => '42'] for /users/42
    (function (string $__file, array $params) {
        include $__file;
    })($match->file, $match->params);
}

PSR-7

match() takes a method and a path string, so any PSR-7 request adapts in one line:

$match = $router->match(
    $request->getMethod(),
    $request->getUri()->getPath()
);

Options

$router = new FileRouter(__DIR__ . '/routes', [
    // Where compiled routes are cached. Omit for no caching (compile per request).
    'cacheDir' => __DIR__ . '/var/cache',
    'cache'    => true,

    // How route files declare their HTTP methods. Default: Flow syntax.
    'methodExtractor' => new \FileRouter\Extractor\FlowMethodExtractor(),
]);

$router->clearCache(); // invalidate the compiled table

Method extractors

A route file can serve several HTTP methods; how that's declared is pluggable via the MethodExtractor interface.

  • FlowMethodExtractor (default) — reads Flow::GET(), Flow::POST(), … declarations (Fluxor style). Flow::ANY() expands to all methods. No dependency on Fluxor; it only reads the file's text.
  • FixedMethodExtractor — every route file serves the same fixed set of methods (e.g. you dispatch by method inside the file).
  • Custom — implement MethodExtractor::extract(string $file): array.

Framework adapters

Testing

composer test

License

MIT

统计信息

  • 总下载量: 0
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 2
  • 依赖项目数: 3
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-06-18

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固