lizzyman04/file-router-symfony
Composer 安装命令:
composer require lizzyman04/file-router-symfony
包简介
Symfony adapter for lizzyman04/file-router — Next.js-style file-based routing via a custom route loader.
README 文档
README
Symfony adapter for lizzyman04/file-router —
Next.js-style file-based routing in Symfony via a custom route loader. The
directory structure becomes a RouteCollection, so file-based routes plug into
Symfony's routing exactly like attribute/annotation or YAML routes — same
matcher, same cache, same dispatch.
Requirements
- PHP 8.1+
- Symfony 6.4 LTS or 7.x
Install
composer require lizzyman04/file-router-symfony
Until the core
lizzyman04/file-routeris published on Packagist, add its repository to your app'scomposer.json:"repositories": [ { "type": "vcs", "url": "https://github.com/lizzyman04/file-router" } ]
Wire up the loader
Register the loader as a service tagged routing.loader:
# config/services.yaml services: FileRouter\Symfony\FileRouterLoader: arguments: $routesPath: '%kernel.project_dir%/routes/pages' tags: ['routing.loader']
Then reference it from your routing config:
# config/routes.yaml file_router: resource: '%kernel.project_dir%/routes/pages' type: file_router
Route files
Each route file returns a callable and declares its HTTP methods with a
// @methods header (defaults to GET):
// routes/pages/users/[id].php <?php // @methods GET, PUT use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Request; return function (Request $request, array $params) { return new JsonResponse([ 'id' => $params['id'], 'method' => $request->getMethod(), ]); };
The handler may return a Symfony Response (passed through) or an array/string
(normalized to a JsonResponse/Response).
Filesystem → URL
| File on disk | Symfony path | Notes |
|---|---|---|
index.php |
/ |
|
about.php |
/about |
|
users/[id].php |
/users/{id} |
$params['id'] |
posts/[id]/comments.php |
/posts/{id}/comments |
|
files/[...path].php |
/files/{path} + requirement path: .+ |
catch-all; $params['path'] is the rest |
(admin)/dashboard.php |
/dashboard |
group folder stripped |
Catch-all is resolved in the loader: [...path] becomes an ordinary Symfony
placeholder constrained to .+, so no custom matching is needed downstream.
Caching & coexistence
- The loader attaches a
DirectoryResource, so Symfony's router cache is invalidated when route files change — no double caching (leave the file-router cache off and let Symfony cache the compiled collection). - The produced routes are merged into Symfony's
RouteCollection, so they coexist with attribute/annotation and YAML routes. 404/405 are produced by Symfony's own matcher.
Testing
composer test
Tests run the loader through Symfony's real UrlMatcher and an HttpKernel
dispatch pipeline.
License
MIT
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 2
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-06-18