1. Go to this page and download the library: Download lucatume/klein52 library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
lucatume / klein52 example snippets
respond(function () {
echo 'Hello World!';
});
function handleAll(){
echo 'Hello World!';
}
respond('handleAll');
respond('/[:name]', function ($request) {
echo 'Hello ' . $request->name;
});
respond('GET', '/posts', $callback);
respond('POST', '/posts/create', $callback);
respond('PUT', '/posts/[i:id]', $callback);
respond('DELETE', '/posts/[i:id]', $callback);
// To match multiple request methods:
respond(array('POST','GET'), $route, $callback);
// Or you might want to handle the requests in the same place
respond('/posts/[create|edit:action]?/[i:id]?', function ($request, $response) {
switch ($request->action) {
//
}
});
respond(function ($request, $response) {
$response->xml = function ($object) {
// Custom xml output function
}
$response->csv = function ($object) {
// Custom csv output function
}
});
respond('/report.[xml|csv|json:format]?', function ($reqest, $response) {
// Get the format or fallback to JSON as the default
$send = $request->param('format', 'json');
$response->$send($report);
});
respond('/report/latest', function ($request, $response) {
$response->file('/tmp/cached_report.zip');
});
respond(function ($request, $response, $app) {
// Handle exceptions => flash the message and redirect to the referrer
$response->onError(function ($response, $err_msg) {
$response->flash($err_msg);
$response->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(...);
});
});
respond('POST', '/users/[i:id]/edit', function ($request, $response) {
// Quickly validate input parameters
$request->validate('username', 'Please enter a valid username')->isLen(5, 64)->isChars('a-zA-Z0-9-');
$request->validate('password')->notNull();
$app->db->query(...); // etc.
// Add view properties and helper methods
$response->title = 'foo';
$response->escape = function ($str) {
return htmlentities($str); // Assign view helpers
};
$response->render('myview.phtml');
});
// myview.phtml:
<title> echo $this->escape($this->title)
inNamespace('/users', function () {
respond('GET', '/?', function ($request, $response) {
// Show all users
});
respond('GET', '/[:id]', function ($request, $response) {
// Show a single user
});
});
foreach(array('projects', 'posts') as $controller) {
inNamespace("/$controller", "controllers/$controller.php");
}
addValidator('hex', function ($str) {
return preg_match('/^[0-9a-f]++$/i', $str);
});
$request->validate('key')->isHex();
$request->validate('key', 'The key was invalid')->isHex()->isLen(32);
respond('*', function ($request, $response) { $response->render('header.phtml'; });
//other routes
respond('*', function ($request, $response) { $response->render('footer.phtml'; });
// Match all requests that end with '.json' or '.csv'
respond('@\.(json|csv)$', ...
// Match all requests that _don't_ start with /admin
respond('!@^/admin/', ...
$response->escape = function ($str) {
return htmlentities($str);
};
$response->render('myview.phtml', array('title' => 'My View'));
// Or just: $response->title = 'My View';
$this->render('partial.html') // Render partials
$this->param('myvar') // Access request parameters
echo $this->query(array('page' => 2)) // Modify the current query string
$request->
header($key, $default = null) // Get a request header
cookie($key, $default = null) // Get a cookie from the request
session($key, $default = null) // Get a session variable
param($key, $default = null) // Get a request parameter (get, post, named)
params() // Return all parameters
params($mask = null) // Return all parameters that match the mask array - extract() friendly
validate($param, $err_msg = null) // Start a validator chain
method() // Get the request method
method($method) // Check if the request method is $method, i.e. method('post') => true
isSecure($essage
file($path, $filename = null) // Send a file
noCache() // Tell the browser not to cache the response
json($object, $jsonp_prefix = null) // Send an object as JSON or JSONP by providing padding prefix
markdown($str, $args, ...) // Return a string formatted with markdown
code($code = null) // Return the HTTP response code, or send a new code
redirect($url, $code = 302) // Redirect to the specified URL
refresh() // Redirect to the current URL
back() // Redirect to the referer
render($view, $data = array()) // Render a view or partial (in the scope of $response)
partial($view, $data = array()) // Render a partial without a layout (in the scope of $response)
layout($layout) // Set the view layout
yieldView() // Call inside the layout to render the view content
error(Exception $err) // Routes an exception through the error callbacks
onError($callback) // $callback takes ($response, $msg, $err_type = null)
set($key, $value = null) // Set a view property or helper
set($arr)
escape($str) // Escape a string
query($key, $value = null) // Modify the current query string
query($arr)
param($param, $default = null) // Get an escaped request parameter
flashes($type = null) // Retrieve and clears all flashes of $type
flush() // Flush all open output buffers
discard($restart_buffer = false) // Discard all open output buffers and optionally restart it
buffer() // Return the contents of the output buffer as a string
chunk($str = null) // Enable response chunking (see the wiki)
dump($obj) // Dump an object
<callback>($arg1, ...) // Call a user-defined helper
<property> // Get a user-defined property
$app->
<callback>($arg1, ...) //Call a user-defined helper
$validator->
notNull() // The string must not be null
isLen($length) // The string must be the exact length
isLen($min, $max) // The string must be between $min and $max length (inclusive)
isInt() // Check for a valid integer
isFloat() // Check for a valid float/decimal
isEmail() // Check for a valid email
isUrl() // Check for a valid URL
isIp() // Check for a valid IP
isAlpha() // Check for a-z (case insensitive)
isAlnum() // Check for alphanumeric characters
contains($needle) // Check if the string contains $needle
isChars($chars) // Validate against a character list
isRegex($pattern, $modifiers = '') // Validate against a regular expression
notRegex($pattern, $modifiers ='')
is<Validator>() // Validate against a custom validator
not<Validator>() // The validator can't match
<Validator>() // Alias for is<Validator>()
php
respond(function ($request, $response, $app) {
$app->register('lazyDb', function() {
$db = new stdClass();
$db->name = 'foo';
return $db;
});
});
//Later
respond('GET', '/posts', function ($request, $response, $app) {
// $db is initialised on first request
// all subsequent calls will use the same instance
echo $app->lazyDb->name;
});
html
<title> echo $this->escape($this->title)
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.