我试图在我的 ZF2 项目中集成控制台模块,该项目目前有 2 个其他工作模块。我已经创建了一个应用程序模块并开始配置路由,就像它在http://framework.zend.com/manual/2.1/en/modules/zend.console.introduction.html中描述的那样
但是如果我从命令行运行php index.php
(有或没有路由参数),我会得到这个又长又奇怪的异常:
PHP Fatal error: Uncaught exception 'Zend\ServiceManager\Exception\ServiceNotFoundException' with message 'Zend\Mvc\Router\RoutePluginManager::get was unable to fetch or create an instance for Segment' in /www/APIO/vendor/zendframework/zendframework/library/Zend/ServiceManager/ServiceManager.php:496
Stack trace:
#0 /www/APIO/vendor/zendframework/zendframework/library/Zend/ServiceManager/AbstractPluginManager.php(103): Zend\ServiceManager\ServiceManager->get('Segment', true)
#1 /www/APIO/vendor/zendframework/zendframework/library/Zend/Mvc/Router/SimpleRouteStack.php(274): Zend\ServiceManager\AbstractPluginManager->get('Segment', Array)
#2 /www/APIO/vendor/zendframework/zendframework/library/Zend/Mvc/Router/Console/SimpleRouteStack.php(78): Zend\Mvc\Router\SimpleRouteStack->routeFromArray(Array)
#3 /www/APIO/vendor/zendframework/zendframework/library/Zend/Mvc/Router/Console/SimpleRouteStack.php(51): Zend\Mvc\Router\Console\SimpleRouteStack->routeFromArray(Array)
#4 /www/APIO/vendor/zendframework/zendframework/library/Zend/Mvc/R in /www/APIO/vendor/zendframework/zendframework/library/Zend/ServiceManager/ServiceManager.php on line 496
Fatal error: Uncaught exception 'Zend\ServiceManager\Exception\ServiceNotFoundException' with message 'Zend\Mvc\Router\RoutePluginManager::get was unable to fetch or create an instance for Segment' in /www/APIO/vendor/zendframework/zendframework/library/Zend/ServiceManager/ServiceManager.php on line 496
Zend\ServiceManager\Exception\ServiceNotFoundException: Zend\Mvc\Router\RoutePluginManager::get was unable to fetch or create an instance for Segment in /www/APIO/vendor/zendframework/zendframework/library/Zend/ServiceManager/ServiceManager.php on line 496
Call Stack:
0.0011 636880 1. {main}() /www/APIO/public/index.php:0
0.0048 933624 2. Zend\Mvc\Application::init() /www/APIO/public/index.php:21
0.0752 6943968 3. Zend\Mvc\Application->bootstrap() /www/APIO/vendor/zendframework/zendframework/library/Zend/Mvc/Application.php:254
0.0815 7543504 4. Zend\ServiceManager\ServiceManager->get() /www/APIO/vendor/zendframework/zendframework/library/Zend/Mvc/Application.php:153
0.0815 7544296 5. Zend\ServiceManager\ServiceManager->create() /www/APIO/vendor/zendframework/zendframework/library/Zend/ServiceManager/ServiceManager.php:481
0.0815 7544296 6. Zend\ServiceManager\ServiceManager->doCreate() /www/APIO/vendor/zendframework/zendframework/library/Zend/ServiceManager/ServiceManager.php:557
0.0816 7544376 7. Zend\ServiceManager\ServiceManager->createFromFactory() /www/APIO/vendor/zendframework/zendframework/library/Zend/ServiceManager/ServiceManager.php:598
0.0819 7565176 8. Zend\ServiceManager\ServiceManager->createServiceViaCallback() /www/APIO/vendor/zendframework/zendframework/library/Zend/ServiceManager/ServiceManager.php:983
我试图用谷歌搜索这个例外,但没有运气。有谁知道这个无法创建的“细分”服务是什么?或者,也许我的应用程序中配置错误?
PS。按照其他一些教程,我的 Module.php 如下所示:
class Module implements AutoloaderProviderInterface,
ConfigProviderInterface,
ConsoleUsageProviderInterface
{
public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}
public function getAutoloaderConfig()
{
return array(
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
),
),
);
}
/**
* Returns a string containing a banner text, that describes the module and/or the application.
* The banner is shown in the console window, when the user supplies invalid command-line parameters or invokes
* the application with no parameters.
*
* The method is called with active Zend\Console\Adapter\AdapterInterface that can be used to directly access Console and send
* output.
*
* @param Console $console
* @return string|null
*/
public function getConsoleBanner(Console $console)
{
// TODO: Implement getConsoleBanner() method.
return 'Application 1.0';
}
public function getConsoleUsage(Console $console)
{
return array(
'show stats' => 'Show application statistics',
'run cron' => 'Run automated jobs',
'(enable|disable) debug' => 'Enable or disable debug mode for the application.'
);
}
}
编辑
这里还有 module.config.php 文件
<?php
return array(
'router' => array(
'routes' => array(
),
'console' => array(
'calculate-distances' => array(
'options' => array(
'route' => 'user calculatedistances [--id=]',
'defaults' => array(
'controller' => 'Application\Controller\Index',
'action' => 'calculate-distances'
)
)
)
)
),
'controllers' => array(
'invokables' => array(
'Application\Controller\Index' => 'Application\Controller\IndexController'
),
),
'service_manager' => array(
'abstract_factories' => array(
'Zend\Cache\Service\StorageCacheAbstractServiceFactory',
'Zend\Log\LoggerAbstractServiceFactory',
),
),
);