PHP code example of sasezaki / backbeard
1. Go to this page and download the library: Download sasezaki/backbeard 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/ */
sasezaki / backbeard example snippets
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use Backbeard\Dispatcher;
use Backbeard\ValidationError;
$routingFactory = function ($container) {
yield '/hello' => 'hello';
$error = (yield ['POST' => '/entry/{id:[0-9]}'] => function ($id) {
if ($this->getRequest()->getPost()['NAME'] == 'wtf') {
return ['var1' => 'baz']; // will be render entry.phtml
} else {
return new ValidationError(['error message!']);
}
});
yield '/entry/{id:[0-9]}' => function ($id) use ($error) {
$message = $error ? htmlspecialchars(current($error->getMessages())) :'';
return "Hello $id ".$message.
'<form method="POST" action="/entry/'.$id.'">'.
'NAME<input type="text" name="NAME">'.
'</form>';
};
yield [
'GET' => '/foo',
'Header' => [
'User-Agent' => function($headers){
if (!empty($headers) && strpos(current($headers), 'Coffee') === 0) {
return true;
}
}
]
] => function () {
return $this->getResponseFactory()->createResponse(418);
}; // status code "I'm a teapot"
yield (ServerRequestInterface $request) {
return true;
} => function () {
/** @var ResponseInterface $this */
return $this;
};
};
(new Dispatcher($routingFactory($container)))->dispatch(new Request);