php [php-lib:Klein] Klein router #php
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php [php-lib:Klein] Klein router #php相关的知识,希望对你有一定的参考价值。
<?php
/*
https://github.com/chriso/klein.php
*/
require_once __DIR__ . '/vendor/autoload.php';
$klein = new \Klein\Klein();
$klein->respond('GET', '/hello-world', function ($request, $response, $service, $app) {
//service data
$service->title = 'Manager';
//Output
return 'Hello World!'; //simple output
$service->render('manager.php'); //render template
});
//parameters - path variables
$klein->respond('PUT', '/posts/[i:id]', $callback); //PUT request
$klein->respond('GET', '/posts/[*:task]', function($request, $response){
//get param value
$action = $request->param('task');
}); //GET
// you might want to handle the requests in the same place
$klein->respond('/[:name]', function ($request, $response) {
$request->action; //action
return 'Hello ' . $request->name;
});
// To match multiple request methods:
$klein->respond(array('POST','GET'), $route, $callback);
//Respond to all requests
$klein->respond(function ($request, $response,$service, $app) {
return 'All the things';
});
//static files.
$klein->respond('/web/[*]', function($request, $response, $service, $app) {
return $response->file(__DIR__ . $request->pathname());
});
/**
Exception handler
*/
// Using exact code behaviors via switch/case
$klein->onHttpError(function ($code, $router) {
switch ($code) {
case 404:
$router->response()->body(
'404 not found!'
);
break;
case 405:
$router->response()->body(
'You can\'t do that!'
);
break;
default:
$router->response()->body(
'Oh no, a bad error happened that caused a '. $code
);
}
});
/*--------------Routes-----------------
/posts/[create|edit:action]?/[i:id]?
/task/[*:task]/?
*/
$klein->dispatch();
以上是关于php [php-lib:Klein] Klein router #php的主要内容,如果未能解决你的问题,请参考以下文章
PHP klein路由器调用特殊控制器功能
[Bilingual]Klein四元群的三个例子Three examples of Klein four-group
Klein 的 Conda 包(Twisted 微型 Web 框架)
Python Klein-非阻塞API
Python Klein - 非阻塞 API
Klein应用程序延期