MGP
MGP

Reputation: 653

ZF2 autoloading custom modules with composer

I'm trying to setup a ZF2 project (slightly different from the zf2skeleton) and I want to use composer to deal with all the autoloading, not only from the vendor ones (installed through composer) but also those modules I create.

For some reason, i can't get access to my Application module Index controller. I think its the autoloading that's not working... I just don't know what i might be doing wrong..

Thanks!

Files and data below:

This is my folder structure:

index.php
private
- modules
-- Aplication
--- src
----- Aplication
------- Controller
---------- IndexController.php
- vendor
-- zendframework
-- composer
-- autoload.php
- composer.json
- composer.phar

main.config.php

return array(
    'modules' => array(
        'Application',
    ),
    'module_listener_options' => array(
        'config_glob_paths' => array(
            'private/config/autoload/{,*.}{global,local}.php',
        ),
        'cache_dir' => realpath(dirname(__DIR__) . '/../../data/cache'),
        'module_paths' => array(
            realpath(__DIR__ . '/../module'),
            realpath(__DIR__ . '/../vendor'),
        ),
    ),
);

index.php

include_once 'private/vendor/autoload.php';

$application = Zend\Mvc\Application::init(include 'private/config/main.config.php');
return $application;

composer.json

{
    "repositories": [
        {
            "type": "composer",
            "url": "http://packages.zendframework.com/"
        }
    ],
    "require": {
        "php": ">=5.3.3",
        "zendframework/zendframework": "2.*"
    },
    "autoload": {
        "psr-0": {
            "Application\\": "module/Application/src"
        }
    }
}

Application config

return array(
    'router' => array(
        'routes' => array(
            'home' => array(
                'type' => 'Zend\Mvc\Router\Http\Literal',
                'options' => array(
                    'route' => '/',
                    'defaults' => array(
                        'controller' => 'Application\Controller\Index',
                        'action' => 'index',
                    ),
                ),
            ),
        ),
    ),
    'controllers' => array(
        'invokables' => array(
            'Application\Controller\Index' => 'Application\Controller\IndexController'
        ),
    ),
);

IndexController

namespace Application\Controller;

use Zend\Mvc\Controller\AbstractActionController;

class IndexController extends AbstractActionController {

    public function indexAction() {
        die("app ok");
    }

}

Upvotes: 0

Views: 3032

Answers (1)

yechabbi
yechabbi

Reputation: 1881

First, from what I see, you don't run your application :

$application = Zend\Mvc\Application::init(include 'private/config/main.config.php');
$application->run();

Another point is that, you don't have to use composer to load your modules. As your application config already include the module directory, the Module loading process will automatically scan the directory and use the loading strategy defined at the level your Module classes.

Upvotes: 1

Related Questions