创建一个没有控制器的zf2模块和一些自动加载的类

时间:2013-01-05 11:05:44

标签: php zend-framework2 zend-loader

我正在尝试创建一个Zend Framework 2模块,该模块包含一些我希望在某些控制器中使用的泛型类。

我在'vendor'目录中创建了一个名为'tux-drink-zf2-module'的目录。

我将该目录添加到'application.config.php'。

我的Module.php有以下内容:

<?php

namespace TuxDrink;

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__ . '/src/' . __NAMESPACE__,
            ),
        ),
    );
}
}

更新

新:

我的autoload_classmap.php包含以下内容:

<?php
// Generated by ZF2's ./bin/classmap_generator.php
return array(
'TuxDrink\Module'                                  => __DIR__ . '/Module.php',
'TuxDbCassandra'                                   => __DIR__ . '/src/TuxDrink/TuxDb/Cassandra/TuxDbCassandra.php',
'TuxDrink\Drink\Model\AlcoholSum'                  => __DIR__ . '/src/TuxDrink/TuxDb/Mysql/Drink/Model/AlcoholSum.php',
'TuxDrink\Drink\Model\AlcoholSumTable'             => __DIR__ . '/src/TuxDrink/TuxDb/Mysql/Drink/Model/AlcoholSumTable.php',
'TuxDrink\Drink\Model\Drink'                       => __DIR__ . '/src/TuxDrink/TuxDb/Mysql/Drink/Model/Drink.php',
'TuxDrink\Drink\Model\DrinkBrand'                  => __DIR__ . '/src/TuxDrink/TuxDb/Mysql/Drink/Model/DrinkBrand.php',
'TuxDrink\Drink\Model\DrinkBrandTable'             => __DIR__ . '/src/TuxDrink/TuxDb/Mysql/Drink/Model/DrinkBrandTable.php',
'TuxDrink\Drink\Model\DrinkCompany'                => __DIR__ . '/src/TuxDrink/TuxDb/Mysql/Drink/Model/DrinkCompany.php',
'TuxDrink\Drink\Model\DrinkCompanyTable'           => __DIR__ . '/src/TuxDrink/TuxDb/Mysql/Drink/Model/DrinkCompanyTable.php',
'TuxDrink\Drink\Model\DrinkFlavor'                 => __DIR__ . '/src/TuxDrink/TuxDb/Mysql/Drink/Model/DrinkFlavor.php',
'TuxDrink\Drink\Model\DrinkFlavorTable'            => __DIR__ . '/src/TuxDrink/TuxDb/Mysql/Drink/Model/DrinkFlavorTable.php',
'TuxDrink\Drink\Model\DrinkFlavorType'             => __DIR__ . '/src/TuxDrink/TuxDb/Mysql/Drink/Model/DrinkFlavorType.php',
'TuxDrink\Drink\Model\DrinkFlavorTypeTable'        => __DIR__ . '/src/TuxDrink/TuxDb/Mysql/Drink/Model/DrinkFlavorTypeTable.php',
'TuxDrink\Drink\Model\DrinkIngredient'             => __DIR__ . '/src/TuxDrink/TuxDb/Mysql/Drink/Model/DrinkIngredient.php',
'TuxDrink\Drink\Model\DrinkIngredientTable'        => __DIR__ . '/src/TuxDrink/TuxDb/Mysql/Drink/Model/DrinkIngredientTable.php',
'TuxDrink\Drink\Model\DrinkIngredientType'         => __DIR__ . '/src/TuxDrink/TuxDb/Mysql/Drink/Model/DrinkIngredientType.php',
'TuxDrink\Drink\Model\DrinkIngredientTypeTable'    => __DIR__ . '/src/TuxDrink/TuxDb/Mysql/Drink/Model/DrinkIngredientTypeTable.php',
'TuxDrink\Drink\Model\DrinkTable'                  => __DIR__ . '/src/TuxDrink/TuxDb/Mysql/Drink/Model/DrinkTable.php',
'TuxDrink\Drink\Model\DrinkType'                   => __DIR__ . '/src/TuxDrink/TuxDb/Mysql/Drink/Model/DrinkType.php',
'TuxDrink\Drink\Model\DrinkTypeTable'              => __DIR__ . '/src/TuxDrink/TuxDb/Mysql/Drink/Model/DrinkTypeTable.php',
'TuxDrink\Drink\Model\GlobalImage'                 => __DIR__ . '/src/TuxDrink/TuxDb/Mysql/Drink/Model/GlobalImage.php',
'TuxDrink\Drink\Model\GlobalImageLink'             => __DIR__ . '/src/TuxDrink/TuxDb/Mysql/Drink/Model/GlobalImageLink.php',
'TuxDrink\Drink\Model\GlobalImageLinkPending'      => __DIR__ . '/src/TuxDrink/TuxDb/Mysql/Drink/Model/GlobalImageLinkPending.php',
'TuxDrink\Drink\Model\GlobalImageLinkPendingTable' => __DIR__ . '/src/TuxDrink/TuxDb/Mysql/Drink/Model/GlobalImageLinkPendingTable.php',
'TuxDrink\Drink\Model\GlobalImageLinkTable'        => __DIR__ . '/src/TuxDrink/TuxDb/Mysql/Drink/Model/GlobalImageLinkTable.php',
'TuxDrink\Drink\Model\GlobalImageTable'            => __DIR__ . '/src/TuxDrink/TuxDb/Mysql/Drink/Model/GlobalImageTable.php',
'TuxDrink\Drink\Model\GlobalImageType'             => __DIR__ . '/src/TuxDrink/TuxDb/Mysql/Drink/Model/GlobalImageType.php',
'TuxDrink\Drink\Model\GlobalImageTypeTable'        => __DIR__ . '/src/TuxDrink/TuxDb/Mysql/Drink/Model/GlobalImageTypeTable.php',
'TuxDrink\Drink\Model\LiquidColor'                 => __DIR__ . '/src/TuxDrink/TuxDb/Mysql/Drink/Model/LiquidColor.php',
'TuxDrink\Drink\Model\LiquidColorTable'            => __DIR__ . '/src/TuxDrink/TuxDb/Mysql/Drink/Model/LiquidColorTable.php',
'TuxDrink\Drink\Model\LiquidColorType'             => __DIR__ . '/src/TuxDrink/TuxDb/Mysql/Drink/Model/LiquidColorType.php',
'TuxDrink\Drink\Model\LiquidColorTypeTable'        => __DIR__ . '/src/TuxDrink/TuxDb/Mysql/Drink/Model/LiquidColorTypeTable.php',
'TuxDrink\Drink\Model\PendingDrinkCommits'         => __DIR__ . '/src/TuxDrink/TuxDb/Mysql/Drink/Model/PendingDrinkCommits.php',
'TuxDrink\Drink\Model\PendingDrinkCommitsTable'    => __DIR__ . '/src/TuxDrink/TuxDb/Mysql/Drink/Model/PendingDrinkCommitsTable.php',
'TuxDrink\TuxDb\Mysql\Drink\TuxDbMysqlDrink'       => __DIR__ . '/src/TuxDrink/TuxDb/Mysql/Drink/TuxDbMysqlDrink.php',
'TuxDrink\TuxDb\Drink\Util\AlcoholCalc'            => __DIR__ . '/src/TuxDrink/TuxDb/Mysql/Drink/Util/AlcoholCalc.php',
'TuxDrink\TuxDb\Mysql\TuxDbMysql'                  => __DIR__ . '/src/TuxDrink/TuxDb/Mysql/TuxDbMysql.php',
'TuxDrink\TuxDb\TuxDbAbstract'                     => __DIR__ . '/src/TuxDrink/TuxDb/TuxDbAbstract.php',
'TuxDrink\TuxDb\TuxDbInterface'                    => __DIR__ . '/src/TuxDrink/TuxDb/TuxDbInterface.php',
'TuxDrink\TuxDb\TuxDrinkInterface'                 => __DIR__ . '/src/TuxDrink/TuxDb/TuxDrinkInterface.php',
'TuxDrink\TuxDb\TuxTableInterface'                 => __DIR__ . '/src/TuxDrink/TuxDb/TuxTableInterface.php',
);

但是当我尝试刷新项目时,我收到以下错误:

 Fatal error: Uncaught exception 'Zend\ModuleManager\Exception\RuntimeException' with message 'Module (tux-drink-zf2-module) could not be initialized.' in /Volumes/2g-storage/projects/php-projects/myalcoholist/vendor/zendframework/zendframework/library/Zend/ModuleManager/ModuleManager.php on line 144

也许我错过了一些其他文件才能将其正确配置为模块? 请记住,这是一个包源,没有控制器,视图和操作。

非常感谢有关该问题的任何信息。

更新

当我修复了目录结构时,这个问题就解决了。

但是当我试图在控制器中使用模块时。

在我的情况下,我需要使用TuxDrink模块中的两个类。

类'TuxDbMysqlDrink'和类'TuxDbMysql'。

我需要在控制器'DrinkController'中使用它们的行动'drink-brand-autocomplete'。

我使用以下代码:

use \TuxDb\Mysql\Drink\TuxDbMysqlDrink;

use \TuxDb\Mysql\TuxDbMysql;


public function drinkBrandAutocompleteAction() {
    $param=$this->getRequest()->getQuery('q');
    if ($param) {
        $drink = new TuxDbMysqlDrink();
        $result=$drink->autoCompleteDrinkBrand($param);
        return $result;
    }
}

我收到以下错误消息:

 Fatal error: DrinkManagement\Controller\DrinkController cannot use TuxDrink\TuxDb\Mysql\Drink\TuxDbMysqlDrink - it is not a trait in /Volumes/2g-storage/projects/php-projects/myalcoholist/module/DrinkManagement/src/DrinkManagement/Controller/DrinkController.php on line 197

任何想法?

尝试谷歌,无法理解这意味着什么“它不是特质”

更新

使用此代码解决了它:

public function drinkBrandAutocompleteAction() {
    $param=$this->getRequest()->getQuery('q');
    if ($param) {
        $drink = new \TuxDrink\TuxDb\Mysql\Drink\TuxDbMysqlDrink();
        $result=$drink->autoCompleteDrinkBrand($param);
        die(var_export($result,1));
     ...

1 个答案:

答案 0 :(得分:2)

根据错误,模块名称为tux-drink-zf2-module。考虑到消毒,我假设您的模块是CamelCased,如TuxDrinkZf2Module。因此,您在Module.php内分配的命名空间不匹配,因为它只是TuxDrink

所以我的猜测是,你的名字都不正确。