Zend Framework 3 получение конфигурации приложения из контроллера
Казалось бы простая вещь, а телодвижений много. Странно. Но тем не менее, решение есть.
Конфигурация приложения:
<?php
return [
'database'=>[
'host' => '127.0.0.1',
'username'=>'root',
'password'=>'perferfre',
'basename'=>'mysql'
],
'modules' => [
'Zend\Router',
'Zend\Validator',
'Application',
],
'module_listener_options' => [
'module_paths' => [
'./module',
'./vendor',
],
'config_cache_enabled' => false,
'config_cache_key' => 'application.config.cache',
'module_map_cache_enabled' => false,
'module_map_cache_key' => 'application.module.cache',
'cache_dir' => 'data/cache/',
],
];
Создаем фабрику IndexControllerFactory.php:
namespace Application\Controller;
use Zend\ServiceManager\Factory\FactoryInterface;
use Interop\Container\ContainerInterface;
class IndexControllerFactory implements FactoryInterface{
public function __invoke(ContainerInterface $container, $requestedName, array $options = null){
$config = $container->get("ApplicationConfig");
return new IndexController($config);
}
}
В конфигурации модуля:
'controllers' => [
'factories' => [
Controller\IndexController::class => Controller\IndexControllerFactory::class,
Controller\UserController::class => InvokableFactory::class,
],
],
В контроллере:
class IndexController extends AbstractActionController{
var $sqln;
public function __construct(array $config) {
var_dump($config);
}
...
