Dependencies

Dependencies are defined in the dependencies.php file in your project folder. Each dependency is accessible to the controllers by its name using $this.

Note

This assumes that your controller extends the base controller
src/MyProject/Controllers/Controller

For example:

dependencies.php

1
2
3
4
5
6
7
<?php
$container['log'] = function () {
    // Log to PHP's default logger (if error_log set in ini)
    $adapter = new ErrorLog(ErrorLog::TYPE_PHP_LOG);
    $log = new Logger($adapter);
    return $log;
};

src/MyProject/Controllers/Index.php

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
<?php
namespace MyProject\Controllers;

class Index extends Controller {

    public function index()
    {
        // $log is defined as $container['log'] in the dependencies.php file
        $log = $this->log;
    }
}