berxam / ranko
用于创建简单API的最小框架。
1.2
2020-09-28 01:55 UTC
This package is auto-updated.
Last update: 2024-09-28 10:44:16 UTC
README
用于创建RESTful API或使用PHP构建简单Web应用的最低框架。
安装
通过 Composer
composer require berxam/ranko
功能
Ranko基本上只是一个路由器,您可以将其挂载到具有相应可调用的路由上,每个可调用都会传递一个Request
和Response
。
- 将控制器函数绑定到HTTP方法和URL或URL模板,如
/users/:id
- 通过
Request
轻松访问请求体和头部 - 使用
Response
方法如sendJSON(mixed $response)
和render(string $view, mixed ...$params)
响应用户
使用方法
了解这个“框架”工作原理的最佳方式是浏览这个仓库中的文件。整个项目不到500行。
Hello world
index.php
:
<?php require_once './vendor/autoload.php'; $app = new Ranko\App; $app->get('/hello', function ($req, $res) { $res->sendJSON(['msg' => 'Hello world!']); }); $app->get('/hello/:world', function ($req, $res) { $world = $req->params['world']; $res->sendHTML("<h1>Hello, $world!</h1>"); }); $app->run(); ?>
请注意,您必须将所有请求定向到index.php
。如果您在Apache服务器上运行PHP,可以使用此.htaccess
重写规则。
RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteRule . index.php [L]