我想从ZendFramework3中的控制器中检索我的模块配置。
$this->getServiceLocator()若要访问module.config.php中的配置,请执行以下操作。但是,这在ZF3中是行不通的,因为没有getServiceLocator()方法。
实现这一目标的标准方法是什么?
发布于 2016-08-31 15:37:11
不知道你是否找到了答案,因为有不同的解决方案,如塔斯马尼斯基所写。以防万一,让我分享一下,当我开始使用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-26 14:55:13
您需要通过服务管理器注入依赖项。基本上,您需要创建两个类,即ControllerFactory、、Controller和,它们将创建具有所有依赖关系的Controller。
https://stackoverflow.com/questions/39163268
复制相似问题