如何在 Zend Framework 2 中加载自定义库? [英] How to load a custom library in Zend Framework 2?
问题描述
我一直在关注本指南(http://socialsemanticweb.blogspot.com.au/2012/11/zend-framework-2-create-custom-library.html)但我无法让 Zend 查看我的图书馆(错误留言下方).
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 message below).
任何想法可能是错误的?谢谢
Any ideas what could be wrong? thanks
我的文件夹结构
我的 MyLibraryController.php
<?php
namespace MyLibrary\Mvc\Controller;
use Zend\Mvc\Controller\AbstractActionController;
class MyLibraryController extends AbstractActionController {
public function __construct() {
}
public function doSomething() {
//instantiate your model here and return result
$result = "test";
return $result;
}
}
我的 autoload_namespaces.php(在 vendor\composer 内)
my autoload_namespaces.php (inside vendor\composer)
<?php
// autoload_namespaces.php generated by Composer
$vendorDir = dirname(__DIR__);
$baseDir = dirname($vendorDir);
return array(
'Zend\\' => $vendorDir . '/zendframework/zendframework/library/',
'ZendTest\\' => $vendorDir . '/zendframework/zendframework/tests/',
'Symfony\\Component\\Console\\' => $vendorDir . '/symfony/console/',
'Doctrine\\ORM' => $vendorDir . '/doctrine/orm/lib/',
'Doctrine\\DBAL' => $vendorDir . '/doctrine/dbal/lib/',
'Doctrine\\Common' => $vendorDir . '/doctrine/common/lib/',
'DoctrineORMModule\\' => $vendorDir . '/doctrine/doctrine-orm-module/src/',
'DoctrineORMModuleTest\\' => $vendorDir . '/doctrine/doctrine-orm-module/tests/',
'DoctrineModule\\' => $vendorDir . '/doctrine/doctrine-module/src/',
'DoctrineModuleTest\\' => $vendorDir . '/doctrine/doctrine-module/tests/',
'MyLibrary\\' => $vendorDir . '/MyLibrary/library/',
);
my application.config.php(我只添加了 MyLibrary 条目.我试过有和没有它)
my application.config.php (I've only added the MyLibrary entry. I've tried with and without it)
<?php
return array(
// This should be an array of module namespaces used in the application.
'modules' => array(
'Application',
'DoctrineModule',
'DoctrineORMModule',
'Directory',
'Helpers',
'MyLibrary',
未在 application.config.php 中添加 MyLibrary 模块的错误消息
Fatal error: Class 'Directory\Controller\MyLibaryController' not found in D:\work\eclipse\htdocs\directory\module\Directory\src\Directory\Controller\DirectoryController.php on line 17
application.config.php 中 MyLibrary 模块条目的错误消息
Fatal error: Uncaught exception 'Zend\ModuleManager\Exception\RuntimeException' with message 'Module (MyLibrary) could not be initialized.' in D:\work\eclipse\htdocs\directory\vendor\zendframework\zendframework\library\Zend\ModuleManager\ModuleManager.php:175 Stack trace: #0 D:\work\eclipse\htdocs\directory\vendor\zendframework\zendframework\library\Zend\ModuleManager\ModuleManager.php(149): Zend\ModuleManager\ModuleManager->loadModuleByName(Object(Zend\ModuleManager\ModuleEvent)) #1 D:\work\eclipse\htdocs\directory\vendor\zendframework\zendframework\library\Zend\ModuleManager\ModuleManager.php(90): Zend\ModuleManager\ModuleManager->loadModule('MyLibrary') #2 [internal function]: Zend\ModuleManager\ModuleManager->onLoadModules(Object(Zend\ModuleManager\ModuleEvent)) #3 D:\work\eclipse\htdocs\directory\vendor\zendframework\zendframework\library\Zend\EventManager\EventManager.php(468): call_user_func(Array, Object(Zend\ModuleManager\ModuleEvent)) #4 D:\work\eclipse\htdocs\directory\vendor\zendframework\zendframework\library in D:\work\eclipse\htdocs\directory\vendor\zendframework\zendframework\library\Zend\ModuleManager\ModuleManager.php on line 175
推荐答案
首先,它不是一个模块,因此通过将它添加到应用配置的 modules 数组中得到的错误消息是意料之中的.
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.
编辑 autoload_namespaces.php
以添加您的库(就像您已经拥有的那样)应该可以工作.
Editing autoload_namespaces.php
to add your library (as you already have) should work.
也就是说,更正确的方法是将 autoload
键添加到您的根 composer.json 文件并在那里进行映射
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/"}
}
}
这样做之后,从命令行运行 composer.phar update
,它会自动将您的库添加到 autoload_namespaces 文件中.这样做意味着您不必每次使用 Composer 更新其他库时都手动编辑文件.
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.
错误本身
致命错误:Class 'Directory\Controller\MyLibaryController' not found in D:\work\eclipse\htdocs\directory\module\Directory\src\Directory\Controller\DirectoryController.php on line 17
Fatal error: Class 'Directory\Controller\MyLibaryController' not found in D:\work\eclipse\htdocs\directory\module\Directory\src\Directory\Controller\DirectoryController.php on line 17
我猜在处理自动加载后,您只是在 DirectoryController
类中缺少一个 use
语句
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
{
//..
}
这篇关于如何在 Zend Framework 2 中加载自定义库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!