-
-
Notifications
You must be signed in to change notification settings - Fork 2
CGI Usage
As visible in this wiki's home, the CGI usage is pretty simple, here how to print a "Hello World!" phrase in the browser:
$mf = require_once('crystal.php');
$mf('/', function () {
echo 'Hello World!';
});
second argument must be callable, so you can use
$mf('/', function () { /* ... */ });
$mf('/', 'function');
$mf('/', array($object, 'method'));
$mf('/', array('Class', 'staticMethod'));
$mf('/', 'Class::staticMethod');
$mf('/', $object); // Used with __invoke
HTTP request methods are the third parameter and are controlled using a regular expression.
$mf('/', function () {
echo 'Hello ' . $_POST['name'] . '!';
}, 'POST');
$mf('/', function () {
echo 'Hello ' . $_SERVER['REQUEST_METHOD'] . '!';
}, 'GET|POST');
Routes are defined using a regular expression too.
$mf('/hello/world', function () {
echo 'Hello ';
});
$mf('/hello(.*)', function () {
echo 'World!';
});
// Output: Hello World!
Use named subpatterns to define parameters order.
$mf('/(?P<user>[^\/]+)/(?P<verb>[^\/]+)', function ($verb, $user) {
echo 'Output: ' . $verb . ' ' . $user . '!';
});
// http://localhost/bob/hello
// Output: hello bob!
Attention: don't forget to configure your webserver!
Priority is used to set callbacks order, each function is called using highest priority 0 but you can set it passing a not negative integer or a not negative float number.
// $mf('/', function () { echo 'A'; });
$mf('/', function () { echo 'A'; }, 'GET', 0);
$mf('/', function () { echo 'B'; }, 'GET', 1);
// Output: AB
$mf('/', function () { echo 'A'; }, 'GET', 1);
$mf('/', function () { echo 'B'; }, 'GET', 0);
// Output: BA
In command line usage priority is the second parameter.
To break callbacks chain use the exit()
function.
The HTTP 404 error is defined as a regular expression too and is matched if no one of currently defined regular expressions match current URL.
To retrieve the 404 regular expression call the function with the argument router:not-found
(available only if not in CLI mode).
$mf('/', function () {
echo 'Hello World!';
});
$mf($mf('router:not-found'), function () {
echo 'Error 404: Page not Found';
});
This is part of the microframework Utilities.