2

Zend Framework 3 のコントローラーからモジュール構成を取得したいのですが、検索したところ、ZF2 でこれを行う標準的な方法は、

$this->getServiceLocator()

の構成にアクセスしますmodule.config.php。ただし、方法がないため、これは ZF3 では機能しませんgetServiceLocator()

これを達成する標準的な方法は何ですか?

4

2 に答える 2

5

タスマニスキが書いたようにさまざまな解決策があるため、答えが見つかったかどうかはわかりません。念のため、私が ZF3 を使い始めたときに大いに役立ったものを紹介しましょう。

MyControllerFactory.php

<?php
namespace My\Namespace;

use Interop\Container\ContainerInterface;
use Zend\ServiceManager\Factory\FactoryInterface;
use DependencyNamespace\...\ControllerDependencyClass; // this is not a real one of course!

class MyControllerFactory implements FactoryInterface
{
    /**
     * @param ContainerInterface $container
     * @param string $requestedName
     * @param null|array $options
     * @return AuthAdapter
     */
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
    {
        // Get config.
        $config = $container->get('configuration');

        // Get what I'm interested in config.
        $myStuff = $config['the-array-i-am-interested-in']

        // Do something with it.
        $controllerDepency = dummyFunction($myStuff);

        /*...the rest of your code here... */

        // Inject dependency.
        return $controllerDepency;
    }
}

MyController.php

<?php
namespace My\Namespace;

use Zend\Mvc\Controller\AbstractActionController;
use DependencyNamespace\...\DependencyClass;

class MyController extends AbstractActionController
{
    private $controllerDepency;

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

    /*...the rest of your class here... */
}
于 2016-08-31T15:37:11.927 に答える