Autoload custom library in Zend Framework 2.0

后端 未结 5 1224
刺人心
刺人心 2020-12-13 16:52

I need to use autoloading for my custom classes in Zend Framework 2.0. My custom library located in /vendor/Garvey/library/Garvey. I have a sim

5条回答
  •  难免孤独
    2020-12-13 17:36

    Have a quick look at this post.

    Now next step is add some code into our custom library.

    First of all open a file ./vendor/Garvey/autoload_classmap.php

    return array(
    
        'Garvey\Module' => __DIR__ . '/Module.php',
    
        'Garvey\Db\Table' => __DIR__ . '/library/Garvey/Db/Table/AbstractTable.php',
    
    )
    

    Next is ./vendor/Garvey/Module.php

    namespace Garvey;
    
    use Zend\ModuleManager\Feature\AutoloaderProviderInterface;
    
    class Module implements AutoloaderProviderInterface
    {
        public function getAutoloaderConfig()
        {
            return array(
                'Zend\Loader\ClassMapAutoloader' => array(
                    __DIR__ . '/autoload_classmap.php',
                ),
    
                'Zend\Loader\StandardAutoloader' => array(
                    'namespaces' => array(
                        __NAMESPACE__ => __DIR__ . '/library/' . __NAMESPACE__,
                    ),
                ),
            );
        }
    }
    

    Now inside your library create a file inside a folder:

    ./vendor/Kdecom/library/Kdecom/Db/Table/AbstractTable.php

    One final thing that we need to do which is add this library into your application.config.php file.

    So your application.config.php file will looks something like this way...

    return array(
        'modules' => array(
            'Application',
            'Garvey'
        ),
    
        'module_listener_options' => array(
            'config_glob_paths'    => array(
                'config/autoload/{,*.}{global,local}.php',
            ),
    
            'module_paths' => array(
                './module',
                './vendor',
            ),
        ),
    );
    

提交回复
热议问题