lizzyman04/file-router-laravel
Composer 安装命令:
composer require lizzyman04/file-router-laravel
包简介
Laravel adapter for lizzyman04/file-router — Next.js-style file-based routing inside Laravel.
README 文档
README
Laravel adapter for lizzyman04/file-router —
Next.js-style file-based routing inside a Laravel app. The directory
structure becomes the route table; routes are registered as native Laravel
routes, so they coexist with routes/web.php / routes/api.php and inherit
Laravel's middleware, container, and route caching.
Dispatch is fully deferred to Laravel's router — this package only resolves which file handles a URL.
Requirements
- PHP 8.2+
- Laravel 11 or 12
Install
composer require lizzyman04/file-router-laravel
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" } ]
Publish the config:
php artisan vendor:publish --tag=file-router-config
// config/file-router.php return [ 'routes_path' => base_path('routes/pages'), // directory scanned for routes 'prefix' => '', // e.g. 'api' to serve under /api/* 'middleware' => ['web'], // middleware applied to all routes 'cache_dir' => null, // e.g. storage_path('framework/cache/file-router') ];
The service provider is auto-discovered.
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 Illuminate\Http\Request; return function (Request $request, array $params) { return response()->json([ 'id' => $params['id'], 'method' => $request->method(), ]); };
Whatever the callable returns is handed to Laravel, which turns it into a
response (arrays → JSON, strings → HTML, Response/Responsable pass through).
Filesystem → URL
| File on disk | URL | 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} (catch-all, .*) |
$params['path'] is the rest of the URL |
(admin)/dashboard.php |
/dashboard |
group folder stripped |
Matching is specificity-ordered: static > dynamic > catch-all.
Middleware & coexistence
- Middleware from the config is applied to every file-based route (they run through Laravel's normal pipeline).
- File-based routes are added to Laravel's route collection on boot, so they
live alongside your
routes/web.phpandroutes/api.phproutes. - Unmatched methods/paths produce Laravel's own 405/404 responses.
Testing
composer test
Tests run against a real Laravel app via orchestra/testbench.
License
MIT
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 2
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-06-18