How to load a custom library in Zend Framework 2?

后端 未结 1 719
一整个雨季
一整个雨季 2021-01-01 17:23

I\'ve been following this guide (http://socialsemanticweb.blogspot.com.au/2012/11/zend-framework-2-create-custom-library.html) but I can\'t get Zend to see my library (error

相关标签:
1条回答
  • 2021-01-01 17:42

    First off, it's not a module, so the error message you get by adding it to the modules array of app config is to be expected.

    Editing autoload_namespaces.php to add your library (as you already have) should work.

    That said, a more correct way is to add the autoload key to your root composer.json file and do the mapping there

    {
        "require": {
            "php": ">=5.3.3",
            "zendframework/zendframework": ">2.2.0rc1"
        },
        "autoload": {
            "psr-0": {"MyLibrary\\": "vendor/MyLibrary/library/"}
        }
    }
    

    After doing that, from the command line run composer.phar update, and it will automatically add your library to the autoload_namespaces file for you. Doing it that way means you don't have to manually edit the file every time you update your other libraries with composer.

    To the error itself

    Fatal error: Class 'Directory\Controller\MyLibaryController' not found in D:\work\eclipse\htdocs\directory\module\Directory\src\Directory\Controller\DirectoryController.php on line 17

    I'm guessing that with autoloading taken care of, you're just missing a use statement in your DirectoryController class

    <?php
    namespace Directory\Controller;
    
    // be sure to use your library controller
    use MyLibrary\Mvc\Controller\MyLibraryController;
    
    class DirectoryController extends MyLibraryController
    {
        //..
    }
    
    0 讨论(0)
提交回复
热议问题