jinxes / jinxkit
此包最新版本(dev-master)没有可用的许可信息。
RESTful 路由器
dev-master
2018-03-25 14:13 UTC
Requires
- php: >=5.5.0
This package is not auto-updated.
Last update: 2024-09-24 08:02:47 UTC
README
最小的RESTful路由器
这是一个迷你PHP路由库。Jinxkit的核心是一个RESTful和面向资源的微内核。它还集成了一个简单的过滤器-控制器系统和DI(依赖注入)容器。
它适合作为某些定制Web框架的基础。
支持的PHP版本
我已经在5.5和7.1(Linux/Windows)上进行了测试,运行正常
安装
composer require jinxes/jinxkit dev-master
开始使用
- 需要Route
require 'vendor/autoload.php'; use Jinxes\Jinxkit\Route;
- 定义控制器并将其连接到路由器
class SayHello { public function say($lang) { echo 'hello ' . $lang; } } Route::get('sayhello/:str', SayHello::class, 'say'); Route::start();
- 开启开发服务器进行测试
php -S localhost:8080
并访问:https://:8080/index.php/sayhello/world
hello world
- 嵌入路由器定义一个嵌入路由器
Route::get('embed/:str', function($word) { echo $word; });
访问:https://:8080/index.php/embed/hello
hello
- 添加一些过滤器定义一个过滤器类并将其添加到SayHello路由器
class Filter { public function __invoke() { $lang = array_shift($this->args); if ($lang === 'world') { echo 'filter pass <br />'; } else { return false; } } } Route::get('sayhello/:str', SayHello::class, 'say')->filter([Filter::class]);
将显示
filter pass
hello world
- 定义RESTful路由器
class SayHello { public function get() { echo 'this is a GET request'; } public function say($lang) { echo 'hello ' . $lang; } } Route::restful('api', SayHello::class, function($router) { $router->get('sayhello/:str', SayHello::class, 'say'); }); Route::start();
访问:https://:8080/index.php/api/sayhello/world
hello world
访问:https://:8080/index.php/api
this is a GET request
过滤器和嵌入式功能也支持DI容器。
- 定义服务类
class SayService { public function hello() { echo 'hello '; } }
服务类是由DI系统维护的一些单例对象
服务的构造也可以注入
- 注入服务
class SayHello { public function say(SayService $sayService, $lang) { echo $sayService->hello() . $lang; } }
您必须将服务放在所有参数之前,并用服务名称声明
访问:https://:8080/index.php/api/sayhello/world
hello world
- 过滤器和嵌入式函数也支持DI容器。