S
S
semki0962017-10-26 13:38:39
Slim Framework
semki096, 2017-10-26 13:38:39

How to work with containers in Slim, what is the logic of building dependencies?

Confused ). Let's say there are 2 objects or modules (what is the correct name?). Twig template engine and Eloquent ORM.
1. I stuff them into containers accordingly

$container['view'] = function ($container) {
    $view = new \Adbar\Slim\Views\Twig('path/to/templates');
    ...
};

$container['db'] = function ($container) {
    $capsule = new \Illuminate\Database\Capsule\Manager;
    ...
}

2. I add a router Here is the question. Do I understand correctly that I must pass instances of Twig and Eloquent objects to this controller? How to do it?
$app->get('/', 'App\PageController:index');

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrew, 2017-10-26
@semki096

Slim itself passes containers to the called controller.
That is, in the controller in the constructor you accept containers:

public function __construct($container) {
        $this->container = $container;
    }

And then you turn to your libs:
$this->container->view;
$this->container->db;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question