我想从 Zend Framework 3 中的 Controller 检索我的模块配置。 我搜索了一下,似乎在 ZF2 中执行此操作的标准方法是使用
$this->getServiceLocator()
访问 module.config.php
中的配置。
但是,这在 ZF3 中不起作用,因为没有 getServiceLocator()
方法。
实现这一目标的标准方法是什么?
最佳答案
不知道您是否找到了答案,因为正如 tasmaniski 所写,有不同的解决方案。为了以防万一,让我分享一个在我开始玩 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... */
}
关于php - 在 ZF3 中获取模块配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39163268/