我一直在关注本指南(http://socialsemanticweb.blogspot.com.au/2012/11/zend-framework-2-create-custom-library.html),但我无法让Zend看到我的图书馆(下面的错误信息)。
任何想法都可能出错?感谢
我的文件夹结构
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中)
<?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/',
);
我的application.config.php (我只添加了MyLibrary条目。我尝试过和没有它)
<?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
答案 0 :(得分:19)
首先,它不是一个模块,因此可以通过将其添加到app config的模块数组中获得错误消息。
编辑autoload_namespaces.php
以添加您的库(正如您已有的)应该可以正常工作。
也就是说,更正确的方法是将autoload
密钥添加到根composer.json文件并在那里进行映射
{
"require": {
"php": ">=5.3.3",
"zendframework/zendframework": ">2.2.0rc1"
},
"autoload": {
"psr-0": {"MyLibrary\\": "vendor/MyLibrary/library/"}
}
}
执行此操作后,从命令行运行composer.phar update
,它将自动将您的库添加到autoload_namespaces文件中。这样做意味着每次使用composer更新其他库时都不必手动编辑文件。
错误本身
致命错误:第17行的D:\ work \ eclipse \ htdocs \ directory \ module \ Directory \ src \ Directory \ Controller \ DirectoryController.php中找不到类'Directory \ Controller \ MyLibaryController'
我猜测,在自动加载的情况下,你只是错过了use
类中的DirectoryController
语句
<?php
namespace Directory\Controller;
// be sure to use your library controller
use MyLibrary\Mvc\Controller\MyLibraryController;
class DirectoryController extends MyLibraryController
{
//..
}