Skip to content
This repository has been archived by the owner on Dec 7, 2019. It is now read-only.

Running with service manager #44

Open
wiryonolau opened this issue Mar 21, 2018 · 4 comments
Open

Running with service manager #44

wiryonolau opened this issue Mar 21, 2018 · 4 comments

Comments

@wiryonolau
Copy link

Could you gave example how to do dependency injection from service manager ( container interop ) that define in the configuration to the handler ?

@jguittard
Copy link

From your console PHP script, chdir to the root, bootstrap composer autoload, and call the container script. Like below:

<?php
use Psr\Container\ContainerInterface;

chdir(__DIR__ . '/../');
require_once 'vendor/autoload.php';

/** @var ContainerInterface $container */
$container = require 'config/container.php';

Later on in the script you can use it in the handler definition:

<?php
use Zend\Console\Console;
// ...

$routes = [
    [
        'name' => 'my-route',
        'route' => '[--foo=]',
        'description' => 'Some really cool feature',
        'short_description' => 'Cool feature',
        'options_descriptions' => [
            'foo'   => 'Lorem Ipsum',
        ],
        'defaults' => [
            'foo'   => 'bar',
        ],
        'handler' => function($route, $console) use ($container) {
            $handler = $container->get(My\Fully\Qualified\Class\Name::class);
            return $handler($route, $console);
        }
    ],
// ...
]

@wiryonolau
Copy link
Author

can dispatcher handler the container (servicemanager) stuff ? this look's dirty. loading container in other file, then on route.config.php reuse the container variable.

@weierophinney
Copy link
Member

Using closures is valid PHP and an acceptable way to provide lazy-loading.

It's also possible to extend the zf-console dispatcher to do this sort of thing, and that would be an interesting patch for us to consider.

@wiryonolau
Copy link
Author

adding handlermanager similar to controllermanager would be nice ( not sure if it is possible to add eventmanager ). handlermanager will use it's own config array instead of putting every handler inside service_manager config.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants