nabeelalihashmi / apilot-engine
PHP的简单端点文件路由器
v0.0.7
2022-01-28 17:55 UTC
This package is auto-updated.
Last update: 2024-09-28 23:50:48 UTC
README
快速自动基于文件的路由器,带有缓存路由
警告
仔细设计API
安装
使用Composer
composer require nabeelalihashmi/apilot-engine
手动
只需下载Apilot.php
文件,将其包含到您的项目中。配置.htaccess
。
<?php
$apilot = new Apilot($handlers_path, $cache_path, $optional_server_basepath);
$apiot->on404(function() {
...
});
$apilot->takeoff();
?>
.htaccess文件
- 将extras/root.htaccess复制到根目录,并将其重命名为.htaccess。
- 将extras/public.htaccess复制到public目录,并将其重命名为.htaccess。
路由
为处理程序创建一个文件夹。该文件夹中的每个文件都对应一个路由。
例如。
cockpit/
index.php -> /
about.php -> /about
contact.php -> contact
...
每个文件至少必须有一个处理程序方法。该方法的名称必须是handle
。
例如。
function GET() {
}
function POST() {
}
function PUT() {
}
动态路由
为了使路由接受动态值,请在路由中使用:
。
例如。
File: cockpit/users/:id/edit.php
URI: /users/:id/edit
function GET($id) {
...
}
s
File: cockpit/users/:id/:action.php
URI: /users/:id/:action
function GET($id, $action) {
...
}
可选参数
为了使路由可选,请使用?
使参数可选。
File: cockpit/list/:page?.php
URI: /list/:page?
function GET($page = 1) {
...
}
规则
- 可选路由必须放置在末尾
hello/:who? Good
hello/:who?/level2 Bad
hello/:who?/level2 Bad
中间件
您可以使用中间件在路由前后执行某些操作。您可以在处理程序方法名后附加before
和after
。before中间件必须返回true以继续。
function GET() {
}
function before_GET() {
return true;
}
function after_GET() {
}
未找到路由
如果路由未找到,Apilot将调用on404
函数。您可以使用此函数重定向到404页面或执行其他操作。
用法
$apilot->on404(function() {
...
});