Home | Github


Table of Contents


Both Exedra\Application and Exedra\Runtime\Context are built as Exedra\Container\Container. This inheritance allows you to set a lazy services, factory creating object and so on.


A service as defined here last for the lifetime of an application or a context, and yield the same value each time retrieved.

use Exedra\Application;

//.. some codes here probably

$app->set('pdo', function(Container $container) {
    $db = $this->config['db'];

    $string = 'mysql:host=' . $db['host'] . ';dbname=' . $db['name'];

    $pdo = new \Pdo($string, $db['user'], $db['pass']);

    return $pdo;

The first argument receives the container itself (either Exedra\Application or Exedra\Runtime\Context).

And you can call it by directly accessing it as public member.

$statement = $app->pdo->query('SELECT * FROM foo');
// or
$statement = $app->get('pdo')->query('SELECT * FROM foo');


This container also allows you to dynamically register an object factory.

$app->factory('@view', function($path) {
    return new \Exedra\View\View($path);

Usage example

return $app->create('view', ['index.php']);


return $context->create('view', ['index.php']);


$app->func('@log', function($message) {

And the callable is invokable on the application instance.

$app->log('Something not well.');

Shared services, factories and callables

Sometimes you might prefer to setup once and share with the context. You may do so by prefixing the name with @.

// this example use Twig templating
$app->set('@twig', function(\Exedra\Application $app) {
    $loader = new \Twig_Loader_Filesystem($app->path->to('views'));

    return new \Twig_Environment($loader);

Usage example

use Exedra\Runtime\Context;

//... some codes here probably

    ->execute(function(Context $context) {
        return $context->twig->render('index.twig');