1
votes

I am very new to Zend Framework 2 and am using the book “Web Development with Zend Framework 2” by Michael Romer as my guide. I’m at the end of chapter 5 and the subject of the ClassMapAutoloader is presented. The conclusion of the discussion is that my Helloworld module now has the file and directory structure of ->

Module.php
autoload_classmap.php
autoload_function.php
autoload_register.php
config/
     module.config.php
public/
     images/
     css/
     js/
src/
     Helloworld/
          Controller/
                   IndexController.php
views/
     Helloworld/
             Index/
                 index.phtml

As far as I can tell the files of interest that setup Classmap autoloading are Module.php, autoload_classmap.php, autoload_function.php, autoload_register.php. The contents of these files are

Module.php ->

<?php

 namespace Helloworld;

 Class Module {

 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() {

      return include __DIR__ . '/config/module.config.php';

 }

 }

autoload_function.php ->

<?php

return function ($class) {

 static $classmap = null;
 if ($classmap === null) {

      $classmap = include __DIR_ . '/autoload_classmap.php';

 }
 if (!isset($classmap[$class])) {

      return false;

 }

 return include_once $classmap[$class];

};

autoload_register.php ->

<?php

spl_autoload_register(include __DIR__  . '/autoload_function.php');

autoload_classmap.php ->

<?php

//require_once 'autoload_register.php';

return array();

This all works when I have that blank array return in autoload_classmap.php BUT in the book the example has require_once 'autoload_register.php';. When I uncomment that line I get the following error ->

The error is -> [Tue Jun 18 16:29:20 2013] [error] [client 199.82.163.121] PHP Fatal error: Uncaught exception 'Zend\Loader\Exception\InvalidArgumentException' with message 'Map file provided does not return a map. Map file: "/var/www/ZendApp/module/Helloworld/autoload_classmap.php"' in /var/www/ZendApp/vendor/zendframework/zendframework/library/Zend/Loader/ClassMapAutoloader.php:88\nStack trace:\n#0 /var/www/ZendApp/vendor/zendframework/zendframework/library/Zend/Loader/ClassMapAutoloader.php(117): Zend\Loader\ClassMapAutoloader->registerAutoloadMap('/var/www/ZendAp...')\n#1 /var/www/ZendApp/vendor/zendframework/zendframework/library/Zend/Loader/ClassMapAutoloader.php(60): Zend\Loader\ClassMapAutoloader->registerAutoloadMaps(Array)\n#2 /var/www/ZendApp/vendor/zendframework/zendframework/library/Zend/Loader/ClassMapAutoloader.php(46): Zend\Loader\ClassMapAutoloader->setOptions(Array)\n#3 /var/www/ZendApp/vendor/zendframework/zendframework/library/Zend/Loader/AutoloaderFactory.php(100): Zend\Loader\ClassMapAutoloader->__construct(Array)\n#4 /var/www/ZendApp/vendor/zendframework/zendframework/library/Zend/M in /var/www/ZendApp/vendor/zendframework/zendframework/library/Zend/Loader/ClassMapAutoloader.php on line 88

I know that returning the blank array causes the getAutoloaderConfig() in the Module class of Module.php to default to the StandardAutoloader and thus it works but Why? I’d really like to get the ClassMapAutoloader to do its thing in this example. How do I get this to work? Thanks in advance for your reply.

James Eastman

2
What is the error? Can you copy/pasteEddie Jaoude
Eddie, Woopsie, forgot the error. Ive edited my original post and included it now. Thanks for the help.James Eastman

2 Answers

3
votes

There is no such thing as requiring the autoloader register function in the classmap file. That is not even in the books.

You can generate the autoloader classmap with the classmap generator provided within Zend Framework 2. You can generate the autoload_classmap.php file so it is populated with all the php classes in your module.

Usage:

$ cd module/MyModule
$ ../../vendor/zendframework/zendframework/bin/classmap_generator.php -w

This works in the case you loaded Zend Framework 2 with composer, which loads the library in the vendor/ directory.

0
votes

in Module.php after name add the two lines of code!

namespace Helloworld; 

    use Zend\Mvc\ModuleRouteListener;
    use Zend\Mvc\MvcEvent;