Wallogit.com
2017 © Pedro Peláez
The one and only PHP 6 framework
The one and only PHP 6 framework., (*1)
Because PHP 6 is the future, but not too much the future at the same time!, (*2)
Oh and also this framework is a very simple introduction to PHP middlewares. It shows a very basic implementation of a framework based on middlewares without PSR-7 or even objects representing the request or the response., (*3)
No., (*4)
No., (*5)
YES!, (*6)
A demo is included in this package (index.php), just run this command and visit http://localhost:8000/index.php, (*7)
docker-compose up
Compiling PHP 6 isn't easy, fortunately you can now deploy it straight to production thanks to this Docker image: wizaplace/php-6-apache., (*8)
Have a look at our docker-compose.yml to see how we use it., (*9)
A traditional middleware looks like this:, (*10)
function (ServerRequestInterface $request, callable $next) {
// do something before the next middleware
$response = $next($request);
// do something after the next middleware
return $response;
}
In thephp6framework, in order to have maximum simplicity and maximum global state (:p) there are no objects to represent the request and the response., (*11)
The request is retrievable via the native PHP way:, (*12)
$_GET, $_POST, $_SERVER, …php://input
The response can be emitted via the native PHP way:, (*13)
echo '...' to output the body (ob_start() and similar functions to buffer the output)header() to set headersBecause of that, a middleware looks like this:, (*14)
function ($next) {
// do something before the next middleware
$next();
// do something after the next middleware
}
Even though the request is not an object, you can still write a middleware that will pre-process it (modify it) before invoking the next middleware:, (*15)
function security_middleware($next) {
// Secure the application BIG TIME by replicating PHP magic quotes
if (\is_array($_GET)) {
foreach ($_GET as &$value) {
$value = \addslashes($value);
}
}
if (\is_array($_POST)) {
foreach ($_POST as &$value) {
$value = \addslashes($value);
}
}
$next();
}
Even though the response is not an object, you can still write a middleware that will modify the response returned by the next middleware:, (*16)
function cloud_to_butt($next) {
// You could also use the framework's capture() function
\ob_start();
$next();
$html = \ob_get_contents();
\ob_end_clean();
$html = \str_replace('the cloud', 'my butt', $html);
echo $html;
}
To build a simple application with different routes, you can use the router() function:, (*17)
require_once __DIR__ . '/theframework.php';
$app = router(array(
'/' => function () {
echo 'The home page';
},
'/about' => function () {
echo 'The about page';
},
));
$app();
You can pipe middlewares one after another using the pipe:, (*18)
$app = pipe(array(
function ($next) {
echo 'Welcome ';
$next();
},
function () {
echo 'in the cloud!';
},
));
$app();
The great thing about thephp6framework is that everything is a middleware:, (*19)
Considering that, you can nest routers in pipes in controllers in pipes in routers…, (*20)
For example, if you want to add middlewares before the router, simply use the pipe:, (*21)
$app = pipe(array(
'security_middleware',
router(array(
'/' => function () {
echo 'Hello world!';
},
)),
));
$app();
capture() functionThe capture() function is a little helper from the framework around output buffering. Instead of this:, (*22)
ob_start(); echo 'Hello world!'; $output = ob_get_contents(); ob_end_clean();
You can write this:, (*23)
$output = capture(function () {
echo 'Hello world!';
});
That means that if you want to capture the output of the next middleware, you can simply do this (because $next is a callable):, (*24)
$html = capture($next);
preg_match()?)This framework is meant as a very simple introduction to PHP middlewares. If you are interested to learn more you can read this great article and have a look at Zend Expressive or Slim., (*25)