Application
At the core of every NaN application stands an instance of the NaN\Application\Interfaces\ApplicationInterface interface,
which extends from Psr\Http\Server\RequestHandlerInterface.
For those running a separate server (e.g. Apache, Nginx, etc.),
NaN provides the NaN\Application\NativeApplication class.
use NaN\Application\NativeApplication as App;
$app = new App();
$app->run();Upon running the application, all request data will be passed on to the middleware in exchange for a response. The response will, therefrom, be sent to the client.
Installation
Section titled “Installation”composer require aimfornan/nanServices
Section titled “Services”Services are to be registered through the App constructor.
It will accept any PSR-11-compatible containers.
NaN provides the Container class from the NaN\DI namespace.
You can learn more about it under dependency injection.
use NaN\DI\Container;
$services = new Container();$app = new App($services);The Container instance can be acquired by middleware through the request’s attributes.
/** * @var Psr\Http\Message\ServerRequestInterface $request * @var Psr\Container\ContainerInterface $services */$services = $request->getAttribute( Psr\Container\ContainerInterface::class,);Every NativeApplication instance makes use of the following services if they exist:
Psr\Http\Message\ServerRequestInterfacePsr\Http\Message\ResponseFactoryInterface.
If they don’t exist, they’ll make use of their own.
Middleware
Section titled “Middleware”App-wide middleware are to be registered through the App constructor.
It will accept any PSR-15-compatible middleware.
use NaN\Application\Middleware\Router\RoutesCollection;
$router = new RoutesCollection();
$app = new App($services, $router);use NaN\Application\Middleware\Router\RoutesCollection;use NaN\Collections\Middleware\MiddlewareCollection;
$middleware = new MiddlewareCollection( new RoutesCollection(),);
$app = new App($services, $middleware);Sharing data between middleware
Section titled “Sharing data between middleware”Sometimes you’ll need middleware to share data among themselves. We recommend using the request’s attributes.
/** @var Psr\Http\Message\ServerRequestInterface $request */$request = $request->withAttribute($name, $value);
/** @var Psr\Http\Server\RequestHandlerInterface $handler */$handler->handle($request);