use Koi\CLI; // Create our application class Application extends Koi\Application { public function __construct() { CLI::$banner = 'Usage: php cli.php [command] [switches]'; CLI::set_opt(NULL, 'help', 'Show this help message'); CLI::set_opt('name', 'name', 'Specify your name', TRUE); } public function index() { CLI::show_help(); } public function name() { if (CLI::get_opt('help') === NULL) { CLI::show_help('name'); exit; } $name = CLI::get_opt('name'); if ($name === FALSE) { CLI::terminate('name', 'You need to specify a name'); } echo "Your name is {$name}" . PHP_EOL; } } $app = new Application(); $app->map('/', 'index'); $app->map('/name', 'name'); $app->run();
<?php require_once __DIR__ . '/../lib/koi.php'; // Create our application class Application extends Koi\Application { public function index() { return "Hello, world!"; } public function not_found() { return array("The requested page could not be found", 404); } } $app = new Application(); $app->map('/', 'index'); $app->map('/:404', 'not_found'); $app->run();
{ return "alphanumeric method"; } public function regex() { return "regex method"; } public function not_found() { return "404 method"; } public function sub_not_found() { return "sub 404 method"; } public function route_args($number) { return "route with args method {$number}"; } } $app = new Application(); $app->map('/', 'index'); $app->map('/:alpha', 'alphabetic'); $app->map('/hello/world', 'sub_route'); $app->map('/:numeric', 'numeric'); $app->map('/:alphanumeric', 'alphanumeric'); $app->map('/koi-([a-zA-Z0-9])+', 'regex'); $app->map('/route_args/:numeric', 'route_args:args'); // Note that in order to prevent collisions 404 routes should be added at the very end. $app->map('/:alphanumeric/:404', 'sub_not_found'); $app->map('/:404', 'not_found');
<?php class Application extends Koi\Application { public function index() { return "Hello, index!"; } public function param($name, $surname) { return "hello {$name} {$surname}"; } } $app = new Application(); $app->map('/', 'index'); $app->map('/param', 'param:args');
<?php class Application extends Koi\Application { public function index() { return array("index method", 200, "text/html"); } public function not_found() { return array("not_found method", 404, "text/html"); } public function no_content() { return array("no_content method", 200); } } $app = new Application(); $app->map('/', 'index'); $app->map('/not_found', 'not_found'); $app->map('/no_content', 'no_content');
require_once __DIR__ . '/../../lib/koi.php'; use Koi\Cookie; class Application extends Koi\Application { public function index() { $c = new Cookie('name', 'Yorick Peterse', NULL, '/', time() + 86400); $c->save(); return $c->value; } public function name() { $c = new Cookie('name'); return $c->value; } public function delete() { $c = new Cookie('name'); if ($c->destroy()) { return "Cookie destroyed"; } else { return "Failed to destroy the cookie"; } } } $app = new Application(); $app->map('/', 'index'); $app->map('/name', 'name'); $app->map('/delete', 'delete'); $app->run();
<?php include 'lib/bootstrap.php'; Application::map('(.*)', 'Welcome'); Application::run();