Skip to content

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.

Terminal window
composer require aimfornan/nan

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\ServerRequestInterface
  • Psr\Http\Message\ResponseFactoryInterface.

If they don’t exist, they’ll make use of their own.

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);

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);