pklink / hahns
此包已被废弃,不再维护。未建议替代包。
PHP 5.4+ 的微框架
0.7.4
2014-02-17 21:58 UTC
Requires
- php: >=5.4.0
- ext-json: *
- pklink/dotor: ~1.0
Requires (Dev)
- codeception/codeception: 1.8.3
README
Hahns

Hahns 是一个适用于 PHP 5.4 及以上版本的微框架。
安装
要使用 composer 安装,请在您的 composer.json 文件中包含以下行。
{
"require": {
"pklink/hahns": "*",
}
}
使用
创建应用
$app = new \Hahns\Hahns();
$app->get('/', function () { return "hello world!"; }); $app->delete('/', function () { return "1"; });
每个对 / 的 GET 请求都将响应
hello world!
每个对 / 的 DELETE 请求都将响应
1
调试模式
要启用调试,请在配置中将 debug 设置为 true
$app = new \Hahns\Hahns(['debug' => true]);
路由回调的参数
Hahns 会根据所需类型自动设置参数
以下是一些内置类型
\Hahns\Hahns\Hahns\Request\Hahns\Response\Html\Hahns\Response\Json\Hahns\Response\Text
$app->get('/', function (\Hahns\Request $request) { // ... }); $app->patch('/', function (\Hahns\Response\Json $response) { // ... }); $app->get('/cars', function (\Hahns\Response\Json $response, \Hahns\Request $request) { // ... });
添加自己的参数
$app->parameter('\\stdClass', function() {
$obj = new stdClass();
$obj->test = 'yup';
return $obj;
});
$app->get('/own/parameter', function (\stdClass $obj) {
return $obj->test;
});
$app->parameter('\Package\MyOwnClass', function(\Hahns\Hahns $app) {
// ...
});
对于 parameter() 的回调必须返回给定类型的实例。
命名参数
基于 正则表达式
$app->get('/hello/[.+:name]', function (\Hahns\Response\Json $response, \Hahns\Request $request) { return $response->send([ 'message' => sprintf('hello %s %s', $request->get('first'), $request->get('last')) }); $app->get('/hello/[.+:first]/[.+:last]', function (\Hahns\Request $request, \Hahns\Response\Json $response) { return $response->send([ 'message' => sprintf('hello %s %s', $request->get('first'), $request->get('last')) ]); }); $app->delete('/cars/id-[\d+:id]/now', function (\Hahns\Response\Json $response, \Hahns\Request $request) { return $response->send([ 'message' => sprintf('removed card with id `%d`', $request->get('id')) ]); });
命名路由
$app->get('/route1', function () { return 'hello world'; }, 'route1'); $app->get('/route2', 'route1', 'route2'); $app->get('/route3', 'route2');
所有对 /route1、/route2 或 /route3 的 GET 请求都会响应
hello world
服务
$app->service('myservice', function(\Hahns\Hahns $app) { $service = new \stdClass(); $service->test = 'hello'; $service->appName = $app->config('name'); return $service; }); $app->get('/service-test', function (\Hahns\Hahns $app) { echo $app->service->test; });
每个对 /service-test 的 GET 请求都会响应
hello
内置服务包括
html-response返回\Hahns\Response\Html实例json-response返回\Hahns\Response\Json实例text-response返回\Hahns\Response\Text实例
事件
Hahns 会触发各种事件。使用 on 方法添加您自己的处理器。
未找到(404)
参数包括
字符串 $usedRoute\Hahns\Hahns $app\Hahns\Exception\NotFoundException $e
$app->on(\Hahns\Hahns::EVENT_NOT_FOUND, function ($usedRoute, \Hahns\Hahns $app, \Hahns\Exception\NotFoundException $e) { // do something });
默认情况下,Hahns发送状态码404
触发“未找到”事件
简单地抛出\Hahns\Exception\NotFoundException
$app->get('/not-found', function () { throw new \Hahns\Exception\NotFoundException(); });
错误
参数包括
\Exception $e\Hahns\Hahns $app
$app->on(\Hahns\Hahns::EVENT_ERROR, function (\Exception $e, \Hahns\Hahns $app) { // do something });
默认情况下,Hahns发送状态码500
触发“错误”事件
简单地抛出\Hahns\Exception\ErrorException
$app->get('/not-found', function () { throw new \Hahns\Exception\NotFoundException(); });
运行前
参数包括
字符串 $givenRoute\Hahns\Hahns $app
$app->on(\Hahns\Hahns::EVENT_BEFORE_RUNNING, function ($givenRoute, \Hahns\Hahns $app) { // do something });
运行后
参数包括
字符串 $usedRoute\Hahns\Hahns $app
$app->on(\Hahns\Hahns::EVENT_AFTER_RUNNING, function ($usedRoute, \Hahns\Hahns $app) { // do something });
路由前
参数包括
字符串 $usedRoute\Hahns\Hahns $app
$app->on(\Hahns\Hahns::EVENT_BEFORE_ROUTING, function ($usedRoute, \Hahns\Hahns $app) { // do something });
路由后
参数包括
字符串 $usedRoute\Hahns\Hahns $app
$app->on(\Hahns\Hahns::EVENT_AFTER_ROUTING, function ($usedRoute, \Hahns\Hahns $app) { // do something });
在执行匹配的路由之前
参数包括
字符串 $usedRoute\Closure $routeCallback数组 $argsForCallback\Hahns\Hahns $app
$app->on(\Hahns\Hahns::EVENT_BEFORE_EXECUTING_ROUTE, function ($usedRoute, \Closure $routeCallback, $argsForCallback, \Hahns\Hahns $app) // do something });
执行匹配的路由之后
参数包括
字符串 $usedRoute\Closure $routeCallback数组 $argsForCallback\Hahns\Hahns $app
$app->on(\Hahns\Hahns::EVENT_AFTER_EXECUTING_ROUTE, function ($usedRoute, \Closure $routeCallback, $argsForCallback, \Hahns\Hahns $app) // do something });
参考
\Hahns\Hahns
void any(string $route, \Closure $callback) // register route for all verbs
void any(string $route, string $namedRoute) // register route for all verbs using the previous route named $namedRoute
void any(string $route, \Closure $callback, string $name) // register routes for all verbs route with name $name
void any(string $route, string $namedRoute, string $name) // register route for all verbs with name $name using the previous route named $namedRoute
mixed config(string $name) // get value $name from config
void delete(string $route, \Closure $callback) // register DELETE-route
void delete(string $route, string $namedRoute) // register DELETE-route using the previous route $namedRoute
void delete(string $route, \Closure $callback, string $name) // register DELETE-route with name $name
void delete(string $route, string $namedRoute, string $name) // register DELETE-route with name $name using the previous route named $namedRoute
void get(string $route, \Closure $callback) // register GET-route
void get(string $route, string $namedRoute) // register GET-route using the previous route $namedRoute
void get(string $route, \Closure $callback, string $name) // register GET-route with name $name
void get(string $route, string $namedRoute, string $name) // register GET-route with name $name using the previous route named $namedRoute
void on(int $event, \Closure $callback) // add handler $callback for event $event
void parameter(string type, \Closure $callback) // register parameter for route callback as singleton
void parameter(string type, \Closure $callback, bool $asSingleton) // register parameter for route callback
void patch(string $route, \Closure $callback) // register PATCH-route
void patch(string $route, string $namedRoute) // register PATCH-route using the previous route $namedRoute
void patch(string $route, \Closure $callback, string $name) // register PATCH-route with name $name
void patch(string $route, string $namedRoute, string $name) // register PATCH-route with name $name using the previous route named $namedRoute
void post(string $route, \Closure $callback) // register POST-route
void post(string $route, string $namedRoute) // register POST-route using the previous route $namedRoute
void post(string $route, \Closure $callback, string $name) // register POST-route with name $name
void post(string $route, string $namedRoute, string $name) // register POST-route with name $name using the previous route named $namedRoute
void put(string $route, \Closure $callback) // register PUT-route
void put(string $route, string $namedRoute) // register PUT-route using the previous route $namedRoute
void put(string $route, \Closure $callback, string $name) // register PUT-route with name $name
void put(string $route, string $namedRoute, string $name) // register PUT-route with name $name using the previous route named $namedRoute
void run() // start routing
void run(string $route) // start routing with the given route $route
void run(string $route, string $requestMethod) // start routing with the given route $route and the request method $requestMethod (useful for simulating request)
object service(string $name) // get service with name $name
void service(string $name, \Closure $callback) // register service
\Hahns\Request
mixed get(string $name) // get GET-param $name or null
mixed get(string $name, mixed $default) // get GET-param $name or $default
mixed header(string $name) // get param $name from request header or null
mixed header(string $name, mixed $default) // get param $name from request header or $default
mixed payload(string $name) // get param $name from payload (DELETE, PATCH, PUT) or null
mixed payload(string $name, mixed $default) // get param $name from payload (DELETE, PATCH, PUT) or $default
mixed post(string $name) // get POST-param $name or null
mixed post(string $name, mixed $default) // get POST-param $name or $default
\Hahns\Response\Html
void header(string $name, string $value) // send header $name with value $value
void redirect(string $location) // send location header with status code 301
void redirect(string $location, int $status) // send location header with status code $code
string send(string $data) // get $data as html
string send(string $data, int $status) // get $data as html and send status code $status to client
void status(int code) // send status code $code with HTTP version 1.1 to client
void status(int code, string $message) // send status code $code with message $message to client
void status(int code, string $message, string $httpVersion) // send status code $code with message $message and HTTP version $version to client
\Hahns\Response\Json
void header(string $name, string $value) // send header $name with value $value
void redirect(string $location) // send location header with status code 301
void redirect(string $location, int $status) // send location header with status code $code
string send(string $data) // get $data as json-decoded string
string send(string $data, int $status) // get $data as json-decoded string and send status code $status to client
void status(int code) // send status code $code with HTTP version 1.1 to client
void status(int code, string $message) // send status code $code with message $message to client
void status(int code, string $message, string $httpVersion) // send status code $code with message $message and HTTP version $version to client
\Hahns\Response\Text
void header(string $name, string $value) // send header $name with value $value
void redirect(string $location) // send location header with status code 301
void redirect(string $location, int $status) // send location header with status code $code
string send(string $data) // get $data as text
string send(string $data, int $status) // get $data as text and send status code $status to client
void status(int code) // send status code $code with HTTP version 1.1 to client
void status(int code, string $message) // send status code $code with message $message to client
void status(int code, string $message, string $httpVersion) // send status code $code with message $message and HTTP version $version to client