S
S
semki0962017-10-23 23:35:03
PHP
semki096, 2017-10-23 23:35:03

How is dependency injection (Slim PHP) implemented in this code?

// Create app
$app = new \Slim\App();

// Get container
$container = $app->getContainer();

// Register component on container
$container['view'] = function ($container) {
    $view = new \Slim\Views\Twig('path/to/templates', [
        'cache' => 'path/to/cache'
    ]);

I want to understand what's going on here. I realized that:
1. there is an app object with a $container container
2. we place a twig object in this container
Do I understand correctly? And most importantly - I can not understand why this function is here and why it passes this argument (function ($ container))?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ilya Gerasimov, 2017-10-24
@semki096

$appthe root of the application, request containers and put a view function there to create a wrapper over the displays. The framework will call the function itself

$container['view'](передаст сюда линк на app->контейнер)
we initiate the view and return the instance to work with the view.
from the controllers we request $this->view->render(...)(magic methods) our wrapper with the method is calledrender

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question