Skip to content

Dependency Container

John Aldrich Bernardo edited this page May 13, 2018 · 2 revisions

Saddle

Saddle is the dependecy container for Calf.

Create

<?php

require_once('vendor/autoload.php');

// Create a new Saddle (Dependency Injector)
$container = new \Calf\Saddle();

// Test message for our Saddle
$container->message = 'Hello World';

// More examples for our Saddle
$container->products = [
    'supplies'  => ['Pen'],
    'fruits'    => ['Apple', 'Pineapple']
];

$container->getProducts = function(\Calf\Saddle $c) {
    return $c->products;
};

// Let's instantiate our application
$app = new \Calf\App($container);

?>

Access

<?php

// Products Service Route Group
$products = new \Calf\HTTP\RouteGroup('products');
$products->add(
    // Test using url:
    // {host}/products/get
    new \Calf\HTTP\Route('/get', function(\Calf\HTTP\Request $req, \Calf\HTTP\Response $res, array $args = []) {
        $res->write($this->getProducts);
        return $res;
    }, ['GET'])
);

Clone this wiki locally