unknownrori / router
支持动态路由的简单路由器
dev-main
2023-04-23 01:05 UTC
Requires (Dev)
- phpunit/phpunit: ^10.0
This package is auto-updated.
Last update: 2024-09-23 05:45:45 UTC
README
这是一个简单的PHP动态路由匹配器,制作这个包的动机是因为我的轻量级框架 Rin 在路由方面表现不佳,我希望它成为一个包,不仅于此,我还想学习在字符串操作中可以完成的字符串匹配和优化。
这里的 Routes
使用了构建者模式,因此注册不会非常痛苦,我计划支持一些实现了 PSR-11
的 Container
,并支持数组,使这个包感觉像使用 Laravel 路由。
在路由匹配方面有一些缺点,传递给 RouteResolver
的URL不能包含查询或主机名,关于查询,你可能可以利用它,但这是未定义的行为。
也可以看看我的其他框架 UnknownRori-PHP。
使用方法
通过composer安装 composer require unknownrori/router
在Laravel上几乎相同,但它感觉更像是一个独立的东西
<?php use UnknownRori\Router\RouteResolver; use UnknownRori\Router\Routes; // Create new instance of `Routes` $routes = new Routes(); // Added default constraint // It will added alphanum, num, alpha // Or you can added it by yourself by calling method `constraint` and passing key and a callable the callable should return a bool $routes->addDefaultConstraints(); // Register some routes and use anonymous function as callback $routes->get("/", fn (string $url) => $url)->name('home'); $routes->get("/posts", fn (string $url) => $url)->name('posts'); $routes->get("/users/{name}", fn (string $name) => "User name called {$name}") ->name('posts.show') ->where(['name' => 'alphanum']); // Create instance of `RouteSolver` $resolver = new RouteResolver($routes); // Resolve the URL // Make sure the URL is looks like this, so you might need to cut some host name and the other stuff to be able properly match the route $result = $resolver->resolve('GET', '/users/UnknownRori'); // Echo the result echo $result;
🛠️ 开发
# clone the repository > git clone https://github.com/UnknownRori/php-router # enter directory > cd php-routes # install dependency > composer install # do some test > composer run test
🌟 贡献
请随意贡献,发送pull request或issue,我会看的