Tags

You can add modules to Zend Framework 2 in two different ways, inside a project in the module directory or outside the project in the vendor directory. This recap deals with vendor packages.

Creating the module

In the vendor folder create a new library. In the Orinoco River project a library with the name hrvanderlingen already exists. The name of the new module is “Chemical”. It should contain a Module.php

namespace Chemical;

use Zend\ModuleManager\Feature\AutoloaderProviderInterface;
use Zend\ModuleManager\Feature\ConfigProviderInterface;

class Module implements AutoloaderProviderInterface, ConfigProviderInterface
{

    public function getAutoloaderConfig()
    {
        return array(
            'Zend\Loader\ClassMapAutoloader' => array(
                __DIR__ . '/autoload_classmap.php',
            ),
            'Zend\Loader\StandardAutoloader' => array(
                'namespaces' => array(
                    __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
                ),
            ),
        );
    }

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

}

and a config/module.config.php

return array(
    'view_manager' => array(
        'template_path_stack' => array(
            'chemistry' => __DIR__ . '/../view',
        ),
    ),
   
    'router' => array(
        'routes' => array(
            'chemistry' => array(
                'type' => 'Literal',
                'options' => array(
                    'route' => '/chemistry',
                    'defaults' => array(
                        'controller' => 'chemistry',
                        'action'     => 'index',
                    ),
                ),               
            ),
        ),
    ),
    'controllers' => array(
        'invokables' => array(
            'Chemistry'
            => 'Chemical\Controller\IndexController'
        ),
    ),
);

The Module.php file defines the namespace for this module, in this case “Chemical”. Note that in the configuration file the module has its own view directory but that the layout is shared with the main application.

Autoload the module

The module is not automatically autoloaded into the application but composer can do this for you. Add the following lines to the main composer.json file:

"autoload": {
        "psr-4":{
            "Chemical\\" : "vendor/hrvanderlingen/chemical"
        }
    }


and run:

composer dump-autoload


this action will update the autoload_psr4.php file in the vendor/composer directory:

$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);

return array(
   
    'Chemical\\' => array($vendorDir . '/hrvanderlingen/chemical'),
);


The content of this file is automatically loaded into the application via the getLoader()method in autoload_real.php

Finally in the main application_config.php add the module to the list of modules

'modules' => array(
        'Application',
        'ZfcBase',
        'ZfcUser',
        'Chemical'
    ),

The module controller should be available with hostname/chemistry

The complete directory structure will look like this:

chemical library layout 2

Repository creation

This module exists inside the main repository but will not be added to the main repository. Instead a new repository will be added as a submodule.
* On Github a new repository is created with the name Chemical
* Remove the existing Chemical directory
* In the vendor folder a submodule is created with:

git.exe submodule add  --force -- "git@github.com:hrvanderlingen/chemical.git"  "vendor/hrvanderlingen/Chemical"

* The .gitmodules file now reads:

[submodule "vendor/hrvanderlingen/Chemical"]
	path = vendor/hrvanderlingen/Chemical
	url = git@github.com:hrvanderlingen/chemical.git

Code available on github

Bugs

An annoying problem can occur with submodule updates:

error: pathspec 'vendor/company/project' did not match any file(s) known to git

One solution is to choose another project name, update the json file and run “composer dump-autoload” again.

Advertisements