简体   繁体   中英

How to configure application.config.php

I am using Zf2,somehow cloned zf3, the application.config.php is still zf2which has my modules configured, but it threw exception for not able to find Route from ServiceManager.

How to add the modules config to my app? The two application config are different.

Thanks,
W.

application.config.php should describe all the Modules the system needs. For example:

return [
    'modules' => [
    'Application',
    'Module1',
    'Module2',
    ],
    'module_listener_options' => [
    'config_glob_paths' => [
        'config/autoload/{,*.}{global,local}.php'
    ],
    'config_cache_enabled' => false,
    'cache_dir' => 'data/cache',
    'module_paths' => [
        './module',
        './vendor'
    ]
    ],
    'service_manager' => [
    'use_defaults' => true,
    'factories' => []
    ]
];

Than ZF will read each module Module.php file and load the associated config files using the method getConfig()

The default is:

public function getConfig()
{
    return include __DIR__ . '/config/module.config.php';
}

For example, you can:

public function getConfig()
{
    $user = include __DIR__ . '/config/user.config.php';
    $group = include __DIR__ . '/config/group.config.php';
    $account = include __DIR__ . '/config/module.config.php';
    return array_merge_recursive($user, $group, $account);
}

I would suggest you download the SkeletonApplication , which is setup for learning purposes. It clearly illustrates and explains the different defaults in the config. Default application.config.php here

In this default config you'll find this:

// Retrieve list of modules used in this application.
'modules' => require __DIR__ . '/modules.config.php',

As such, you have a separate modules.config.php next to the application.config.php to separate these concerns. By default it looks like this:

<?php
/**
 * @link      http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository
 * @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 */
/**
 * List of enabled modules for this application.
 *
 * This should be an array of module namespaces used in the application.
 */
return [
    'Zend\Router',
    'Zend\Validator',
    'Application',
];

Though after you run composer install more will like be added here.

In the Skeleton's only module ( Application ), you'll also find a Module.php in the src/ folder. This is where module config gets loaded from/by.

This is where I would recommend you start to deviate a bit from the default setup, by creating your own AbstractModule class in a generic module (MVC?) somewhere. I recommend this to minimize the amount of duplicated code, as most modules will not have more than a simple "here's the config, do your thing", like the default linked above.

My own Module.php classes look like this:

class Module extends AbstractModule
{
    public function __construct()
    {
        parent::__construct(__DIR__, __NAMESPACE__);
    }
}

And the AbstractModule is this:

abstract class AbstractModule implements ConfigProviderInterface, AutoloaderProviderInterface
{
    /**
     * @var String Path of current module
     */
    protected $path;

    /**
     * @var String Namespace of current module
     */
    protected $namespace;

    /**
     * This is to be called by descendant classes with:
     * parent::__construct(__DIR__, __NAMESPACE__)
     *
     * @param $path      string Module path
     * @param $namespace string Module namespace
     */
    public function __construct($path, $namespace)
    {
        $this->path = $path;
        $this->namespace = $namespace;
    }

    /**
     * @return array
     */
    public function getConfig()
    {
        $config = [];

        $path = $this->path 
            . DIRECTORY_SEPARATOR . '..' 
            . DIRECTORY_SEPARATOR . 'config' 
            . DIRECTORY_SEPARATOR . '*.php';

        foreach (glob($path) as $filename) {
            $config = array_merge_recursive($config, include $filename);
        }

        return $config;
    }

    /**
     * @return array
     */
    public function getAutoloaderConfig()
    {
        return [
            'Zend\Loader\StandardAutoloader' => [
                'namespaces' => [
                    $this->namespace => $this->path . DIRECTORY_SEPARATOR . 'src',
                ],
            ],
        ];
    }
}

This setup would still allow you to modify the Module.php of a specific module and it's generic in such a way that you require only that __construct function the child-classes if you need do nothing else.

Note, this setup takes any .php files in the config/ folder. So you could develop your own packages to include with default config .dist files and still use this class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM