Getting started
Introduction
Getting started with Concise which is using Functional programming style should be straightforward.
The Philosophy behind Concise is to remove unnecessary abstractions and complexity from creating apps. It is relatively a light weight library in that it does not have many modules built into it. However it allows for adding a lot of extra functionality by providing a fairly simple API.
Hello world
Get ready to create your first Concise application. Below is the Hello world example
<?php
require './vendor/autoload.php';
use function Concise\app;
use function Concise\Routing\get;
use function Concise\Http\Response\response;
use function Concise\Http\Request\url;
use function Concise\Http\Request\path;
use function Concise\Middleware\Factory\create as createMiddleware;
use function Concise\FP\curry;
use function Concise\FP\ifElse;
function createLogger()
{
$outputFileHandler = fopen('php://stdout', 'w');
return function (string $message, array $context = null) use ($outputFileHandler) {
fwrite($outputFileHandler, $message);
return $context;
};
}
/**
* Curried logger which logs message and returns the context
*
* @param string $message Message to output
* @param array $context to be passed to the next function
* @return mixed Either the curried function or the array context
*/
function logger(...$thisArgs)
{
static $logger = null;
if (!$logger) {
$logger = createLogger();
}
return curry($logger)(...$thisArgs);
}
function loggerMiddleware()
{
return createMiddleware(function (callable $nextRouteHandler, array $middlewareParams = [], array $request) {
return $nextRouteHandler(ifElse(
function($request) {
return $request['meta']['hasRouteMatch'];
},
logger("\n\nRoute with path".path().' matching. Params are: '.implode(',', $request['params'])."\n"),
logger("\nNo route matching for: ".url(). "\n")
)(logger("\n\nRequest: ".json_encode($request))($request)));
});
}
function routes()
{
return [
get('/hello/:name')(function ($request) {
return response('Welcome to Concise, ' . $request['params']['name'], []);
})
];
}
function middlewares()
{
return [
loggerMiddleware()
];
}
logger("\nResponse: ".json_encode(app(routes())(middlewares()))."\n")([]);