轻量级PHP路由库:Klein
jopen
11年前
Klein 是一款针对 PHP5.3+ 版本的轻量级路由库,速度相当快。
-
灵活的正则表达式路由规则 (灵感来自 Sinatra)
-
提供一组样板方法用于快速构建 Web 应用
-
几乎没有开销 => 2500+ requests/second
示例代码:
Hello World - Obligatory hello world example
<?php require_once __DIR__ . '/vendor/autoload.php'; $klein = new \Klein\Klein(); $klein->respond('GET', '/hello-world', function () { return 'Hello World!'; }); $klein->dispatch();
Example 1 - Respond to all requests
$klein->respond(function () { return 'All the things'; });
Example 2 - Named parameters
$klein->respond('/[:name]', function ($request) { return 'Hello ' . $request->name; });
Example 3 - So RESTful
$klein->respond('GET', '/posts', $callback); $klein->respond('POST', '/posts/create', $callback); $klein->respond('PUT', '/posts/[i:id]', $callback); $klein->respond('DELETE', '/posts/[i:id]', $callback); $klein->respond('OPTIONS', null, $callback); // To match multiple request methods: $klein->respond(array('POST','GET'), $route, $callback); // Or you might want to handle the requests in the same place $klein->respond('/posts/[create|edit:action]?/[i:id]?', function ($request, $response) { switch ($request->action) { // } });
Example 4 - Sending objects / files
$klein->respond(function ($request, $response, $service) { $service->xml = function ($object) { // Custom xml output function } $service->csv = function ($object) { // Custom csv output function } }); $klein->respond('/report.[xml|csv|json:format]?', function ($request, $response, $service) { // Get the format or fallback to JSON as the default $send = $request->param('format', 'json'); $service->$send($report); }); $klein->respond('/report/latest', function ($request, $response, $service) { $service->file('/tmp/cached_report.zip'); });
Example 5 - All together
$klein->respond(function ($request, $response, $service, $app) use ($klein) { // Handle exceptions => flash the message and redirect to the referrer $klein->onError(function ($klein, $err_msg) { $klein->service()->flash($err_msg); $klein->service()->back(); }); // The third parameter can be used to share scope and global objects $app->db = new PDO(...); // $app also can store lazy services, e.g. if you don't want to // instantiate a database connection on every response $app->register('db', function() { return new PDO(...); }); }); $klein->respond('POST', '/users/[i:id]/edit', function ($request, $response, $service, $app) { // Quickly validate input parameters $service->validateParam('username', 'Please enter a valid username')->isLen(5, 64)->isChars('a-zA-Z0-9-'); $service->validateParam('password')->notNull(); $app->db->query(...); // etc. // Add view properties and helper methods $service->title = 'foo'; $service->escape = function ($str) { return htmlentities($str); // Assign view helpers }; $service->render('myview.phtml'); }); // myview.phtml: <title><?php echo $this->escape($this->title) ?></title>
Route namespaces
$klein->with('/users', function () use ($klein) { $klein->respond('GET', '/?', function ($request, $response) { // Show all users }); $klein->respond('GET', '/[:id]', function ($request, $response) { // Show a single user }); }); foreach(array('projects', 'posts') as $controller) { // Include all routes defined in a file under a given namespace $klein->with("/$controller", "controllers/$controller.php"); }